ncr53c8xx.c 212 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679
  1. /******************************************************************************
  2. ** Device driver for the PCI-SCSI NCR538XX controller family.
  3. **
  4. ** Copyright (C) 1994 Wolfgang Stanglmeier
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. **
  20. **-----------------------------------------------------------------------------
  21. **
  22. ** This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
  23. ** and is currently maintained by
  24. **
  25. ** Gerard Roudier <groudier@free.fr>
  26. **
  27. ** Being given that this driver originates from the FreeBSD version, and
  28. ** in order to keep synergy on both, any suggested enhancements and corrections
  29. ** received on Linux are automatically a potential candidate for the FreeBSD
  30. ** version.
  31. **
  32. ** The original driver has been written for 386bsd and FreeBSD by
  33. ** Wolfgang Stanglmeier <wolf@cologne.de>
  34. ** Stefan Esser <se@mi.Uni-Koeln.de>
  35. **
  36. ** And has been ported to NetBSD by
  37. ** Charles M. Hannum <mycroft@gnu.ai.mit.edu>
  38. **
  39. **-----------------------------------------------------------------------------
  40. **
  41. ** Brief history
  42. **
  43. ** December 10 1995 by Gerard Roudier:
  44. ** Initial port to Linux.
  45. **
  46. ** June 23 1996 by Gerard Roudier:
  47. ** Support for 64 bits architectures (Alpha).
  48. **
  49. ** November 30 1996 by Gerard Roudier:
  50. ** Support for Fast-20 scsi.
  51. ** Support for large DMA fifo and 128 dwords bursting.
  52. **
  53. ** February 27 1997 by Gerard Roudier:
  54. ** Support for Fast-40 scsi.
  55. ** Support for on-Board RAM.
  56. **
  57. ** May 3 1997 by Gerard Roudier:
  58. ** Full support for scsi scripts instructions pre-fetching.
  59. **
  60. ** May 19 1997 by Richard Waltham <dormouse@farsrobt.demon.co.uk>:
  61. ** Support for NvRAM detection and reading.
  62. **
  63. ** August 18 1997 by Cort <cort@cs.nmt.edu>:
  64. ** Support for Power/PC (Big Endian).
  65. **
  66. ** June 20 1998 by Gerard Roudier
  67. ** Support for up to 64 tags per lun.
  68. ** O(1) everywhere (C and SCRIPTS) for normal cases.
  69. ** Low PCI traffic for command handling when on-chip RAM is present.
  70. ** Aggressive SCSI SCRIPTS optimizations.
  71. **
  72. ** 2005 by Matthew Wilcox and James Bottomley
  73. ** PCI-ectomy. This driver now supports only the 720 chip (see the
  74. ** NCR_Q720 and zalon drivers for the bus probe logic).
  75. **
  76. *******************************************************************************
  77. */
  78. /*
  79. ** Supported SCSI-II features:
  80. ** Synchronous negotiation
  81. ** Wide negotiation (depends on the NCR Chip)
  82. ** Enable disconnection
  83. ** Tagged command queuing
  84. ** Parity checking
  85. ** Etc...
  86. **
  87. ** Supported NCR/SYMBIOS chips:
  88. ** 53C720 (Wide, Fast SCSI-2, intfly problems)
  89. */
  90. /* Name and version of the driver */
  91. #define SCSI_NCR_DRIVER_NAME "ncr53c8xx-3.4.3g"
  92. #define SCSI_NCR_DEBUG_FLAGS (0)
  93. #include <linux/blkdev.h>
  94. #include <linux/delay.h>
  95. #include <linux/dma-mapping.h>
  96. #include <linux/errno.h>
  97. #include <linux/init.h>
  98. #include <linux/interrupt.h>
  99. #include <linux/ioport.h>
  100. #include <linux/mm.h>
  101. #include <linux/module.h>
  102. #include <linux/sched.h>
  103. #include <linux/signal.h>
  104. #include <linux/spinlock.h>
  105. #include <linux/stat.h>
  106. #include <linux/string.h>
  107. #include <linux/time.h>
  108. #include <linux/timer.h>
  109. #include <linux/types.h>
  110. #include <asm/dma.h>
  111. #include <asm/io.h>
  112. #include <asm/system.h>
  113. #include <scsi/scsi.h>
  114. #include <scsi/scsi_cmnd.h>
  115. #include <scsi/scsi_dbg.h>
  116. #include <scsi/scsi_device.h>
  117. #include <scsi/scsi_tcq.h>
  118. #include <scsi/scsi_transport.h>
  119. #include <scsi/scsi_transport_spi.h>
  120. #include "ncr53c8xx.h"
  121. #define NAME53C8XX "ncr53c8xx"
  122. /*==========================================================
  123. **
  124. ** Debugging tags
  125. **
  126. **==========================================================
  127. */
  128. #define DEBUG_ALLOC (0x0001)
  129. #define DEBUG_PHASE (0x0002)
  130. #define DEBUG_QUEUE (0x0008)
  131. #define DEBUG_RESULT (0x0010)
  132. #define DEBUG_POINTER (0x0020)
  133. #define DEBUG_SCRIPT (0x0040)
  134. #define DEBUG_TINY (0x0080)
  135. #define DEBUG_TIMING (0x0100)
  136. #define DEBUG_NEGO (0x0200)
  137. #define DEBUG_TAGS (0x0400)
  138. #define DEBUG_SCATTER (0x0800)
  139. #define DEBUG_IC (0x1000)
  140. /*
  141. ** Enable/Disable debug messages.
  142. ** Can be changed at runtime too.
  143. */
  144. #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
  145. static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
  146. #define DEBUG_FLAGS ncr_debug
  147. #else
  148. #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
  149. #endif
  150. static inline struct list_head *ncr_list_pop(struct list_head *head)
  151. {
  152. if (!list_empty(head)) {
  153. struct list_head *elem = head->next;
  154. list_del(elem);
  155. return elem;
  156. }
  157. return NULL;
  158. }
  159. /*==========================================================
  160. **
  161. ** Simple power of two buddy-like allocator.
  162. **
  163. ** This simple code is not intended to be fast, but to
  164. ** provide power of 2 aligned memory allocations.
  165. ** Since the SCRIPTS processor only supplies 8 bit
  166. ** arithmetic, this allocator allows simple and fast
  167. ** address calculations from the SCRIPTS code.
  168. ** In addition, cache line alignment is guaranteed for
  169. ** power of 2 cache line size.
  170. ** Enhanced in linux-2.3.44 to provide a memory pool
  171. ** per pcidev to support dynamic dma mapping. (I would
  172. ** have preferred a real bus astraction, btw).
  173. **
  174. **==========================================================
  175. */
  176. #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
  177. #if PAGE_SIZE >= 8192
  178. #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
  179. #else
  180. #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
  181. #endif
  182. #define MEMO_FREE_UNUSED /* Free unused pages immediately */
  183. #define MEMO_WARN 1
  184. #define MEMO_GFP_FLAGS GFP_ATOMIC
  185. #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
  186. #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
  187. #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
  188. typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
  189. typedef struct device *m_bush_t; /* Something that addresses DMAable */
  190. typedef struct m_link { /* Link between free memory chunks */
  191. struct m_link *next;
  192. } m_link_s;
  193. typedef struct m_vtob { /* Virtual to Bus address translation */
  194. struct m_vtob *next;
  195. m_addr_t vaddr;
  196. m_addr_t baddr;
  197. } m_vtob_s;
  198. #define VTOB_HASH_SHIFT 5
  199. #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
  200. #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
  201. #define VTOB_HASH_CODE(m) \
  202. ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
  203. typedef struct m_pool { /* Memory pool of a given kind */
  204. m_bush_t bush;
  205. m_addr_t (*getp)(struct m_pool *);
  206. void (*freep)(struct m_pool *, m_addr_t);
  207. int nump;
  208. m_vtob_s *(vtob[VTOB_HASH_SIZE]);
  209. struct m_pool *next;
  210. struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
  211. } m_pool_s;
  212. static void *___m_alloc(m_pool_s *mp, int size)
  213. {
  214. int i = 0;
  215. int s = (1 << MEMO_SHIFT);
  216. int j;
  217. m_addr_t a;
  218. m_link_s *h = mp->h;
  219. if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
  220. return NULL;
  221. while (size > s) {
  222. s <<= 1;
  223. ++i;
  224. }
  225. j = i;
  226. while (!h[j].next) {
  227. if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
  228. h[j].next = (m_link_s *)mp->getp(mp);
  229. if (h[j].next)
  230. h[j].next->next = NULL;
  231. break;
  232. }
  233. ++j;
  234. s <<= 1;
  235. }
  236. a = (m_addr_t) h[j].next;
  237. if (a) {
  238. h[j].next = h[j].next->next;
  239. while (j > i) {
  240. j -= 1;
  241. s >>= 1;
  242. h[j].next = (m_link_s *) (a+s);
  243. h[j].next->next = NULL;
  244. }
  245. }
  246. #ifdef DEBUG
  247. printk("___m_alloc(%d) = %p\n", size, (void *) a);
  248. #endif
  249. return (void *) a;
  250. }
  251. static void ___m_free(m_pool_s *mp, void *ptr, int size)
  252. {
  253. int i = 0;
  254. int s = (1 << MEMO_SHIFT);
  255. m_link_s *q;
  256. m_addr_t a, b;
  257. m_link_s *h = mp->h;
  258. #ifdef DEBUG
  259. printk("___m_free(%p, %d)\n", ptr, size);
  260. #endif
  261. if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
  262. return;
  263. while (size > s) {
  264. s <<= 1;
  265. ++i;
  266. }
  267. a = (m_addr_t) ptr;
  268. while (1) {
  269. #ifdef MEMO_FREE_UNUSED
  270. if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
  271. mp->freep(mp, a);
  272. break;
  273. }
  274. #endif
  275. b = a ^ s;
  276. q = &h[i];
  277. while (q->next && q->next != (m_link_s *) b) {
  278. q = q->next;
  279. }
  280. if (!q->next) {
  281. ((m_link_s *) a)->next = h[i].next;
  282. h[i].next = (m_link_s *) a;
  283. break;
  284. }
  285. q->next = q->next->next;
  286. a = a & b;
  287. s <<= 1;
  288. ++i;
  289. }
  290. }
  291. static DEFINE_SPINLOCK(ncr53c8xx_lock);
  292. static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
  293. {
  294. void *p;
  295. p = ___m_alloc(mp, size);
  296. if (DEBUG_FLAGS & DEBUG_ALLOC)
  297. printk ("new %-10s[%4d] @%p.\n", name, size, p);
  298. if (p)
  299. memset(p, 0, size);
  300. else if (uflags & MEMO_WARN)
  301. printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
  302. return p;
  303. }
  304. #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
  305. static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
  306. {
  307. if (DEBUG_FLAGS & DEBUG_ALLOC)
  308. printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
  309. ___m_free(mp, ptr, size);
  310. }
  311. /*
  312. * With pci bus iommu support, we use a default pool of unmapped memory
  313. * for memory we donnot need to DMA from/to and one pool per pcidev for
  314. * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
  315. */
  316. static m_addr_t ___mp0_getp(m_pool_s *mp)
  317. {
  318. m_addr_t m = __get_free_pages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER);
  319. if (m)
  320. ++mp->nump;
  321. return m;
  322. }
  323. static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
  324. {
  325. free_pages(m, MEMO_PAGE_ORDER);
  326. --mp->nump;
  327. }
  328. static m_pool_s mp0 = {NULL, ___mp0_getp, ___mp0_freep};
  329. /*
  330. * DMAable pools.
  331. */
  332. /*
  333. * With pci bus iommu support, we maintain one pool per pcidev and a
  334. * hashed reverse table for virtual to bus physical address translations.
  335. */
  336. static m_addr_t ___dma_getp(m_pool_s *mp)
  337. {
  338. m_addr_t vp;
  339. m_vtob_s *vbp;
  340. vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
  341. if (vbp) {
  342. dma_addr_t daddr;
  343. vp = (m_addr_t) dma_alloc_coherent(mp->bush,
  344. PAGE_SIZE<<MEMO_PAGE_ORDER,
  345. &daddr, GFP_ATOMIC);
  346. if (vp) {
  347. int hc = VTOB_HASH_CODE(vp);
  348. vbp->vaddr = vp;
  349. vbp->baddr = daddr;
  350. vbp->next = mp->vtob[hc];
  351. mp->vtob[hc] = vbp;
  352. ++mp->nump;
  353. return vp;
  354. }
  355. }
  356. if (vbp)
  357. __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
  358. return 0;
  359. }
  360. static void ___dma_freep(m_pool_s *mp, m_addr_t m)
  361. {
  362. m_vtob_s **vbpp, *vbp;
  363. int hc = VTOB_HASH_CODE(m);
  364. vbpp = &mp->vtob[hc];
  365. while (*vbpp && (*vbpp)->vaddr != m)
  366. vbpp = &(*vbpp)->next;
  367. if (*vbpp) {
  368. vbp = *vbpp;
  369. *vbpp = (*vbpp)->next;
  370. dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
  371. (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
  372. __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
  373. --mp->nump;
  374. }
  375. }
  376. static inline m_pool_s *___get_dma_pool(m_bush_t bush)
  377. {
  378. m_pool_s *mp;
  379. for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
  380. return mp;
  381. }
  382. static m_pool_s *___cre_dma_pool(m_bush_t bush)
  383. {
  384. m_pool_s *mp;
  385. mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
  386. if (mp) {
  387. memset(mp, 0, sizeof(*mp));
  388. mp->bush = bush;
  389. mp->getp = ___dma_getp;
  390. mp->freep = ___dma_freep;
  391. mp->next = mp0.next;
  392. mp0.next = mp;
  393. }
  394. return mp;
  395. }
  396. static void ___del_dma_pool(m_pool_s *p)
  397. {
  398. struct m_pool **pp = &mp0.next;
  399. while (*pp && *pp != p)
  400. pp = &(*pp)->next;
  401. if (*pp) {
  402. *pp = (*pp)->next;
  403. __m_free(&mp0, p, sizeof(*p), "MPOOL");
  404. }
  405. }
  406. static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
  407. {
  408. u_long flags;
  409. struct m_pool *mp;
  410. void *m = NULL;
  411. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  412. mp = ___get_dma_pool(bush);
  413. if (!mp)
  414. mp = ___cre_dma_pool(bush);
  415. if (mp)
  416. m = __m_calloc(mp, size, name);
  417. if (mp && !mp->nump)
  418. ___del_dma_pool(mp);
  419. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  420. return m;
  421. }
  422. static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
  423. {
  424. u_long flags;
  425. struct m_pool *mp;
  426. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  427. mp = ___get_dma_pool(bush);
  428. if (mp)
  429. __m_free(mp, m, size, name);
  430. if (mp && !mp->nump)
  431. ___del_dma_pool(mp);
  432. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  433. }
  434. static m_addr_t __vtobus(m_bush_t bush, void *m)
  435. {
  436. u_long flags;
  437. m_pool_s *mp;
  438. int hc = VTOB_HASH_CODE(m);
  439. m_vtob_s *vp = NULL;
  440. m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
  441. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  442. mp = ___get_dma_pool(bush);
  443. if (mp) {
  444. vp = mp->vtob[hc];
  445. while (vp && (m_addr_t) vp->vaddr != a)
  446. vp = vp->next;
  447. }
  448. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  449. return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
  450. }
  451. #define _m_calloc_dma(np, s, n) __m_calloc_dma(np->dev, s, n)
  452. #define _m_free_dma(np, p, s, n) __m_free_dma(np->dev, p, s, n)
  453. #define m_calloc_dma(s, n) _m_calloc_dma(np, s, n)
  454. #define m_free_dma(p, s, n) _m_free_dma(np, p, s, n)
  455. #define _vtobus(np, p) __vtobus(np->dev, p)
  456. #define vtobus(p) _vtobus(np, p)
  457. /*
  458. * Deal with DMA mapping/unmapping.
  459. */
  460. /* To keep track of the dma mapping (sg/single) that has been set */
  461. #define __data_mapped SCp.phase
  462. #define __data_mapping SCp.have_data_in
  463. static void __unmap_scsi_data(struct device *dev, struct scsi_cmnd *cmd)
  464. {
  465. switch(cmd->__data_mapped) {
  466. case 2:
  467. dma_unmap_sg(dev, cmd->buffer, cmd->use_sg,
  468. cmd->sc_data_direction);
  469. break;
  470. case 1:
  471. dma_unmap_single(dev, cmd->__data_mapping,
  472. cmd->request_bufflen,
  473. cmd->sc_data_direction);
  474. break;
  475. }
  476. cmd->__data_mapped = 0;
  477. }
  478. static u_long __map_scsi_single_data(struct device *dev, struct scsi_cmnd *cmd)
  479. {
  480. dma_addr_t mapping;
  481. if (cmd->request_bufflen == 0)
  482. return 0;
  483. mapping = dma_map_single(dev, cmd->request_buffer,
  484. cmd->request_bufflen,
  485. cmd->sc_data_direction);
  486. cmd->__data_mapped = 1;
  487. cmd->__data_mapping = mapping;
  488. return mapping;
  489. }
  490. static int __map_scsi_sg_data(struct device *dev, struct scsi_cmnd *cmd)
  491. {
  492. int use_sg;
  493. if (cmd->use_sg == 0)
  494. return 0;
  495. use_sg = dma_map_sg(dev, cmd->buffer, cmd->use_sg,
  496. cmd->sc_data_direction);
  497. cmd->__data_mapped = 2;
  498. cmd->__data_mapping = use_sg;
  499. return use_sg;
  500. }
  501. #define unmap_scsi_data(np, cmd) __unmap_scsi_data(np->dev, cmd)
  502. #define map_scsi_single_data(np, cmd) __map_scsi_single_data(np->dev, cmd)
  503. #define map_scsi_sg_data(np, cmd) __map_scsi_sg_data(np->dev, cmd)
  504. /*==========================================================
  505. **
  506. ** Driver setup.
  507. **
  508. ** This structure is initialized from linux config
  509. ** options. It can be overridden at boot-up by the boot
  510. ** command line.
  511. **
  512. **==========================================================
  513. */
  514. static struct ncr_driver_setup
  515. driver_setup = SCSI_NCR_DRIVER_SETUP;
  516. #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
  517. static struct ncr_driver_setup
  518. driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
  519. #endif
  520. #define initverbose (driver_setup.verbose)
  521. #define bootverbose (np->verbose)
  522. /*===================================================================
  523. **
  524. ** Driver setup from the boot command line
  525. **
  526. **===================================================================
  527. */
  528. #ifdef MODULE
  529. #define ARG_SEP ' '
  530. #else
  531. #define ARG_SEP ','
  532. #endif
  533. #define OPT_TAGS 1
  534. #define OPT_MASTER_PARITY 2
  535. #define OPT_SCSI_PARITY 3
  536. #define OPT_DISCONNECTION 4
  537. #define OPT_SPECIAL_FEATURES 5
  538. #define OPT_UNUSED_1 6
  539. #define OPT_FORCE_SYNC_NEGO 7
  540. #define OPT_REVERSE_PROBE 8
  541. #define OPT_DEFAULT_SYNC 9
  542. #define OPT_VERBOSE 10
  543. #define OPT_DEBUG 11
  544. #define OPT_BURST_MAX 12
  545. #define OPT_LED_PIN 13
  546. #define OPT_MAX_WIDE 14
  547. #define OPT_SETTLE_DELAY 15
  548. #define OPT_DIFF_SUPPORT 16
  549. #define OPT_IRQM 17
  550. #define OPT_PCI_FIX_UP 18
  551. #define OPT_BUS_CHECK 19
  552. #define OPT_OPTIMIZE 20
  553. #define OPT_RECOVERY 21
  554. #define OPT_SAFE_SETUP 22
  555. #define OPT_USE_NVRAM 23
  556. #define OPT_EXCLUDE 24
  557. #define OPT_HOST_ID 25
  558. #ifdef SCSI_NCR_IARB_SUPPORT
  559. #define OPT_IARB 26
  560. #endif
  561. static char setup_token[] __initdata =
  562. "tags:" "mpar:"
  563. "spar:" "disc:"
  564. "specf:" "ultra:"
  565. "fsn:" "revprob:"
  566. "sync:" "verb:"
  567. "debug:" "burst:"
  568. "led:" "wide:"
  569. "settle:" "diff:"
  570. "irqm:" "pcifix:"
  571. "buschk:" "optim:"
  572. "recovery:"
  573. "safe:" "nvram:"
  574. "excl:" "hostid:"
  575. #ifdef SCSI_NCR_IARB_SUPPORT
  576. "iarb:"
  577. #endif
  578. ; /* DONNOT REMOVE THIS ';' */
  579. #ifdef MODULE
  580. #define ARG_SEP ' '
  581. #else
  582. #define ARG_SEP ','
  583. #endif
  584. static int __init get_setup_token(char *p)
  585. {
  586. char *cur = setup_token;
  587. char *pc;
  588. int i = 0;
  589. while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
  590. ++pc;
  591. ++i;
  592. if (!strncmp(p, cur, pc - cur))
  593. return i;
  594. cur = pc;
  595. }
  596. return 0;
  597. }
  598. static int __init sym53c8xx__setup(char *str)
  599. {
  600. #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
  601. char *cur = str;
  602. char *pc, *pv;
  603. int i, val, c;
  604. int xi = 0;
  605. while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
  606. char *pe;
  607. val = 0;
  608. pv = pc;
  609. c = *++pv;
  610. if (c == 'n')
  611. val = 0;
  612. else if (c == 'y')
  613. val = 1;
  614. else
  615. val = (int) simple_strtoul(pv, &pe, 0);
  616. switch (get_setup_token(cur)) {
  617. case OPT_TAGS:
  618. driver_setup.default_tags = val;
  619. if (pe && *pe == '/') {
  620. i = 0;
  621. while (*pe && *pe != ARG_SEP &&
  622. i < sizeof(driver_setup.tag_ctrl)-1) {
  623. driver_setup.tag_ctrl[i++] = *pe++;
  624. }
  625. driver_setup.tag_ctrl[i] = '\0';
  626. }
  627. break;
  628. case OPT_MASTER_PARITY:
  629. driver_setup.master_parity = val;
  630. break;
  631. case OPT_SCSI_PARITY:
  632. driver_setup.scsi_parity = val;
  633. break;
  634. case OPT_DISCONNECTION:
  635. driver_setup.disconnection = val;
  636. break;
  637. case OPT_SPECIAL_FEATURES:
  638. driver_setup.special_features = val;
  639. break;
  640. case OPT_FORCE_SYNC_NEGO:
  641. driver_setup.force_sync_nego = val;
  642. break;
  643. case OPT_REVERSE_PROBE:
  644. driver_setup.reverse_probe = val;
  645. break;
  646. case OPT_DEFAULT_SYNC:
  647. driver_setup.default_sync = val;
  648. break;
  649. case OPT_VERBOSE:
  650. driver_setup.verbose = val;
  651. break;
  652. case OPT_DEBUG:
  653. driver_setup.debug = val;
  654. break;
  655. case OPT_BURST_MAX:
  656. driver_setup.burst_max = val;
  657. break;
  658. case OPT_LED_PIN:
  659. driver_setup.led_pin = val;
  660. break;
  661. case OPT_MAX_WIDE:
  662. driver_setup.max_wide = val? 1:0;
  663. break;
  664. case OPT_SETTLE_DELAY:
  665. driver_setup.settle_delay = val;
  666. break;
  667. case OPT_DIFF_SUPPORT:
  668. driver_setup.diff_support = val;
  669. break;
  670. case OPT_IRQM:
  671. driver_setup.irqm = val;
  672. break;
  673. case OPT_PCI_FIX_UP:
  674. driver_setup.pci_fix_up = val;
  675. break;
  676. case OPT_BUS_CHECK:
  677. driver_setup.bus_check = val;
  678. break;
  679. case OPT_OPTIMIZE:
  680. driver_setup.optimize = val;
  681. break;
  682. case OPT_RECOVERY:
  683. driver_setup.recovery = val;
  684. break;
  685. case OPT_USE_NVRAM:
  686. driver_setup.use_nvram = val;
  687. break;
  688. case OPT_SAFE_SETUP:
  689. memcpy(&driver_setup, &driver_safe_setup,
  690. sizeof(driver_setup));
  691. break;
  692. case OPT_EXCLUDE:
  693. if (xi < SCSI_NCR_MAX_EXCLUDES)
  694. driver_setup.excludes[xi++] = val;
  695. break;
  696. case OPT_HOST_ID:
  697. driver_setup.host_id = val;
  698. break;
  699. #ifdef SCSI_NCR_IARB_SUPPORT
  700. case OPT_IARB:
  701. driver_setup.iarb = val;
  702. break;
  703. #endif
  704. default:
  705. printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
  706. break;
  707. }
  708. if ((cur = strchr(cur, ARG_SEP)) != NULL)
  709. ++cur;
  710. }
  711. #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
  712. return 1;
  713. }
  714. /*===================================================================
  715. **
  716. ** Get device queue depth from boot command line.
  717. **
  718. **===================================================================
  719. */
  720. #define DEF_DEPTH (driver_setup.default_tags)
  721. #define ALL_TARGETS -2
  722. #define NO_TARGET -1
  723. #define ALL_LUNS -2
  724. #define NO_LUN -1
  725. static int device_queue_depth(int unit, int target, int lun)
  726. {
  727. int c, h, t, u, v;
  728. char *p = driver_setup.tag_ctrl;
  729. char *ep;
  730. h = -1;
  731. t = NO_TARGET;
  732. u = NO_LUN;
  733. while ((c = *p++) != 0) {
  734. v = simple_strtoul(p, &ep, 0);
  735. switch(c) {
  736. case '/':
  737. ++h;
  738. t = ALL_TARGETS;
  739. u = ALL_LUNS;
  740. break;
  741. case 't':
  742. if (t != target)
  743. t = (target == v) ? v : NO_TARGET;
  744. u = ALL_LUNS;
  745. break;
  746. case 'u':
  747. if (u != lun)
  748. u = (lun == v) ? v : NO_LUN;
  749. break;
  750. case 'q':
  751. if (h == unit &&
  752. (t == ALL_TARGETS || t == target) &&
  753. (u == ALL_LUNS || u == lun))
  754. return v;
  755. break;
  756. case '-':
  757. t = ALL_TARGETS;
  758. u = ALL_LUNS;
  759. break;
  760. default:
  761. break;
  762. }
  763. p = ep;
  764. }
  765. return DEF_DEPTH;
  766. }
  767. /*==========================================================
  768. **
  769. ** The CCB done queue uses an array of CCB virtual
  770. ** addresses. Empty entries are flagged using the bogus
  771. ** virtual address 0xffffffff.
  772. **
  773. ** Since PCI ensures that only aligned DWORDs are accessed
  774. ** atomically, 64 bit little-endian architecture requires
  775. ** to test the high order DWORD of the entry to determine
  776. ** if it is empty or valid.
  777. **
  778. ** BTW, I will make things differently as soon as I will
  779. ** have a better idea, but this is simple and should work.
  780. **
  781. **==========================================================
  782. */
  783. #define SCSI_NCR_CCB_DONE_SUPPORT
  784. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  785. #define MAX_DONE 24
  786. #define CCB_DONE_EMPTY 0xffffffffUL
  787. /* All 32 bit architectures */
  788. #if BITS_PER_LONG == 32
  789. #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
  790. /* All > 32 bit (64 bit) architectures regardless endian-ness */
  791. #else
  792. #define CCB_DONE_VALID(cp) \
  793. ((((u_long) cp) & 0xffffffff00000000ul) && \
  794. (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
  795. #endif
  796. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  797. /*==========================================================
  798. **
  799. ** Configuration and Debugging
  800. **
  801. **==========================================================
  802. */
  803. /*
  804. ** SCSI address of this device.
  805. ** The boot routines should have set it.
  806. ** If not, use this.
  807. */
  808. #ifndef SCSI_NCR_MYADDR
  809. #define SCSI_NCR_MYADDR (7)
  810. #endif
  811. /*
  812. ** The maximum number of tags per logic unit.
  813. ** Used only for disk devices that support tags.
  814. */
  815. #ifndef SCSI_NCR_MAX_TAGS
  816. #define SCSI_NCR_MAX_TAGS (8)
  817. #endif
  818. /*
  819. ** TAGS are actually limited to 64 tags/lun.
  820. ** We need to deal with power of 2, for alignment constraints.
  821. */
  822. #if SCSI_NCR_MAX_TAGS > 64
  823. #define MAX_TAGS (64)
  824. #else
  825. #define MAX_TAGS SCSI_NCR_MAX_TAGS
  826. #endif
  827. #define NO_TAG (255)
  828. /*
  829. ** Choose appropriate type for tag bitmap.
  830. */
  831. #if MAX_TAGS > 32
  832. typedef u64 tagmap_t;
  833. #else
  834. typedef u32 tagmap_t;
  835. #endif
  836. /*
  837. ** Number of targets supported by the driver.
  838. ** n permits target numbers 0..n-1.
  839. ** Default is 16, meaning targets #0..#15.
  840. ** #7 .. is myself.
  841. */
  842. #ifdef SCSI_NCR_MAX_TARGET
  843. #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
  844. #else
  845. #define MAX_TARGET (16)
  846. #endif
  847. /*
  848. ** Number of logic units supported by the driver.
  849. ** n enables logic unit numbers 0..n-1.
  850. ** The common SCSI devices require only
  851. ** one lun, so take 1 as the default.
  852. */
  853. #ifdef SCSI_NCR_MAX_LUN
  854. #define MAX_LUN SCSI_NCR_MAX_LUN
  855. #else
  856. #define MAX_LUN (1)
  857. #endif
  858. /*
  859. ** Asynchronous pre-scaler (ns). Shall be 40
  860. */
  861. #ifndef SCSI_NCR_MIN_ASYNC
  862. #define SCSI_NCR_MIN_ASYNC (40)
  863. #endif
  864. /*
  865. ** The maximum number of jobs scheduled for starting.
  866. ** There should be one slot per target, and one slot
  867. ** for each tag of each target in use.
  868. ** The calculation below is actually quite silly ...
  869. */
  870. #ifdef SCSI_NCR_CAN_QUEUE
  871. #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
  872. #else
  873. #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
  874. #endif
  875. /*
  876. ** We limit the max number of pending IO to 250.
  877. ** since we donnot want to allocate more than 1
  878. ** PAGE for 'scripth'.
  879. */
  880. #if MAX_START > 250
  881. #undef MAX_START
  882. #define MAX_START 250
  883. #endif
  884. /*
  885. ** The maximum number of segments a transfer is split into.
  886. ** We support up to 127 segments for both read and write.
  887. ** The data scripts are broken into 2 sub-scripts.
  888. ** 80 (MAX_SCATTERL) segments are moved from a sub-script
  889. ** in on-chip RAM. This makes data transfers shorter than
  890. ** 80k (assuming 1k fs) as fast as possible.
  891. */
  892. #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
  893. #if (MAX_SCATTER > 80)
  894. #define MAX_SCATTERL 80
  895. #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
  896. #else
  897. #define MAX_SCATTERL (MAX_SCATTER-1)
  898. #define MAX_SCATTERH 1
  899. #endif
  900. /*
  901. ** other
  902. */
  903. #define NCR_SNOOP_TIMEOUT (1000000)
  904. /*
  905. ** Other definitions
  906. */
  907. #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
  908. #define initverbose (driver_setup.verbose)
  909. #define bootverbose (np->verbose)
  910. /*==========================================================
  911. **
  912. ** Command control block states.
  913. **
  914. **==========================================================
  915. */
  916. #define HS_IDLE (0)
  917. #define HS_BUSY (1)
  918. #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
  919. #define HS_DISCONNECT (3) /* Disconnected by target */
  920. #define HS_DONEMASK (0x80)
  921. #define HS_COMPLETE (4|HS_DONEMASK)
  922. #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
  923. #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
  924. #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
  925. #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
  926. #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
  927. #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
  928. /*
  929. ** Invalid host status values used by the SCRIPTS processor
  930. ** when the nexus is not fully identified.
  931. ** Shall never appear in a CCB.
  932. */
  933. #define HS_INVALMASK (0x40)
  934. #define HS_SELECTING (0|HS_INVALMASK)
  935. #define HS_IN_RESELECT (1|HS_INVALMASK)
  936. #define HS_STARTING (2|HS_INVALMASK)
  937. /*
  938. ** Flags set by the SCRIPT processor for commands
  939. ** that have been skipped.
  940. */
  941. #define HS_SKIPMASK (0x20)
  942. /*==========================================================
  943. **
  944. ** Software Interrupt Codes
  945. **
  946. **==========================================================
  947. */
  948. #define SIR_BAD_STATUS (1)
  949. #define SIR_XXXXXXXXXX (2)
  950. #define SIR_NEGO_SYNC (3)
  951. #define SIR_NEGO_WIDE (4)
  952. #define SIR_NEGO_FAILED (5)
  953. #define SIR_NEGO_PROTO (6)
  954. #define SIR_REJECT_RECEIVED (7)
  955. #define SIR_REJECT_SENT (8)
  956. #define SIR_IGN_RESIDUE (9)
  957. #define SIR_MISSING_SAVE (10)
  958. #define SIR_RESEL_NO_MSG_IN (11)
  959. #define SIR_RESEL_NO_IDENTIFY (12)
  960. #define SIR_RESEL_BAD_LUN (13)
  961. #define SIR_RESEL_BAD_TARGET (14)
  962. #define SIR_RESEL_BAD_I_T_L (15)
  963. #define SIR_RESEL_BAD_I_T_L_Q (16)
  964. #define SIR_DONE_OVERFLOW (17)
  965. #define SIR_INTFLY (18)
  966. #define SIR_MAX (18)
  967. /*==========================================================
  968. **
  969. ** Extended error codes.
  970. ** xerr_status field of struct ccb.
  971. **
  972. **==========================================================
  973. */
  974. #define XE_OK (0)
  975. #define XE_EXTRA_DATA (1) /* unexpected data phase */
  976. #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
  977. /*==========================================================
  978. **
  979. ** Negotiation status.
  980. ** nego_status field of struct ccb.
  981. **
  982. **==========================================================
  983. */
  984. #define NS_NOCHANGE (0)
  985. #define NS_SYNC (1)
  986. #define NS_WIDE (2)
  987. #define NS_PPR (4)
  988. /*==========================================================
  989. **
  990. ** Misc.
  991. **
  992. **==========================================================
  993. */
  994. #define CCB_MAGIC (0xf2691ad2)
  995. /*==========================================================
  996. **
  997. ** Declaration of structs.
  998. **
  999. **==========================================================
  1000. */
  1001. static struct scsi_transport_template *ncr53c8xx_transport_template = NULL;
  1002. struct tcb;
  1003. struct lcb;
  1004. struct ccb;
  1005. struct ncb;
  1006. struct script;
  1007. struct link {
  1008. ncrcmd l_cmd;
  1009. ncrcmd l_paddr;
  1010. };
  1011. struct usrcmd {
  1012. u_long target;
  1013. u_long lun;
  1014. u_long data;
  1015. u_long cmd;
  1016. };
  1017. #define UC_SETSYNC 10
  1018. #define UC_SETTAGS 11
  1019. #define UC_SETDEBUG 12
  1020. #define UC_SETORDER 13
  1021. #define UC_SETWIDE 14
  1022. #define UC_SETFLAG 15
  1023. #define UC_SETVERBOSE 17
  1024. #define UF_TRACE (0x01)
  1025. #define UF_NODISC (0x02)
  1026. #define UF_NOSCAN (0x04)
  1027. /*========================================================================
  1028. **
  1029. ** Declaration of structs: target control block
  1030. **
  1031. **========================================================================
  1032. */
  1033. struct tcb {
  1034. /*----------------------------------------------------------------
  1035. ** During reselection the ncr jumps to this point with SFBR
  1036. ** set to the encoded target number with bit 7 set.
  1037. ** if it's not this target, jump to the next.
  1038. **
  1039. ** JUMP IF (SFBR != #target#), @(next tcb)
  1040. **----------------------------------------------------------------
  1041. */
  1042. struct link jump_tcb;
  1043. /*----------------------------------------------------------------
  1044. ** Load the actual values for the sxfer and the scntl3
  1045. ** register (sync/wide mode).
  1046. **
  1047. ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
  1048. ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
  1049. **----------------------------------------------------------------
  1050. */
  1051. ncrcmd getscr[6];
  1052. /*----------------------------------------------------------------
  1053. ** Get the IDENTIFY message and load the LUN to SFBR.
  1054. **
  1055. ** CALL, <RESEL_LUN>
  1056. **----------------------------------------------------------------
  1057. */
  1058. struct link call_lun;
  1059. /*----------------------------------------------------------------
  1060. ** Now look for the right lun.
  1061. **
  1062. ** For i = 0 to 3
  1063. ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
  1064. **
  1065. ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
  1066. ** It is kind of hashcoding.
  1067. **----------------------------------------------------------------
  1068. */
  1069. struct link jump_lcb[4]; /* JUMPs for reselection */
  1070. struct lcb * lp[MAX_LUN]; /* The lcb's of this tcb */
  1071. /*----------------------------------------------------------------
  1072. ** Pointer to the ccb used for negotiation.
  1073. ** Prevent from starting a negotiation for all queued commands
  1074. ** when tagged command queuing is enabled.
  1075. **----------------------------------------------------------------
  1076. */
  1077. struct ccb * nego_cp;
  1078. /*----------------------------------------------------------------
  1079. ** statistical data
  1080. **----------------------------------------------------------------
  1081. */
  1082. u_long transfers;
  1083. u_long bytes;
  1084. /*----------------------------------------------------------------
  1085. ** negotiation of wide and synch transfer and device quirks.
  1086. **----------------------------------------------------------------
  1087. */
  1088. #ifdef SCSI_NCR_BIG_ENDIAN
  1089. /*0*/ u16 period;
  1090. /*2*/ u_char sval;
  1091. /*3*/ u_char minsync;
  1092. /*0*/ u_char wval;
  1093. /*1*/ u_char widedone;
  1094. /*2*/ u_char quirks;
  1095. /*3*/ u_char maxoffs;
  1096. #else
  1097. /*0*/ u_char minsync;
  1098. /*1*/ u_char sval;
  1099. /*2*/ u16 period;
  1100. /*0*/ u_char maxoffs;
  1101. /*1*/ u_char quirks;
  1102. /*2*/ u_char widedone;
  1103. /*3*/ u_char wval;
  1104. #endif
  1105. /* User settable limits and options. */
  1106. u_char usrsync;
  1107. u_char usrwide;
  1108. u_char usrtags;
  1109. u_char usrflag;
  1110. struct scsi_target *starget;
  1111. };
  1112. /*========================================================================
  1113. **
  1114. ** Declaration of structs: lun control block
  1115. **
  1116. **========================================================================
  1117. */
  1118. struct lcb {
  1119. /*----------------------------------------------------------------
  1120. ** During reselection the ncr jumps to this point
  1121. ** with SFBR set to the "Identify" message.
  1122. ** if it's not this lun, jump to the next.
  1123. **
  1124. ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
  1125. **
  1126. ** It is this lun. Load TEMP with the nexus jumps table
  1127. ** address and jump to RESEL_TAG (or RESEL_NOTAG).
  1128. **
  1129. ** SCR_COPY (4), p_jump_ccb, TEMP,
  1130. ** SCR_JUMP, <RESEL_TAG>
  1131. **----------------------------------------------------------------
  1132. */
  1133. struct link jump_lcb;
  1134. ncrcmd load_jump_ccb[3];
  1135. struct link jump_tag;
  1136. ncrcmd p_jump_ccb; /* Jump table bus address */
  1137. /*----------------------------------------------------------------
  1138. ** Jump table used by the script processor to directly jump
  1139. ** to the CCB corresponding to the reselected nexus.
  1140. ** Address is allocated on 256 bytes boundary in order to
  1141. ** allow 8 bit calculation of the tag jump entry for up to
  1142. ** 64 possible tags.
  1143. **----------------------------------------------------------------
  1144. */
  1145. u32 jump_ccb_0; /* Default table if no tags */
  1146. u32 *jump_ccb; /* Virtual address */
  1147. /*----------------------------------------------------------------
  1148. ** CCB queue management.
  1149. **----------------------------------------------------------------
  1150. */
  1151. struct list_head free_ccbq; /* Queue of available CCBs */
  1152. struct list_head busy_ccbq; /* Queue of busy CCBs */
  1153. struct list_head wait_ccbq; /* Queue of waiting for IO CCBs */
  1154. struct list_head skip_ccbq; /* Queue of skipped CCBs */
  1155. u_char actccbs; /* Number of allocated CCBs */
  1156. u_char busyccbs; /* CCBs busy for this lun */
  1157. u_char queuedccbs; /* CCBs queued to the controller*/
  1158. u_char queuedepth; /* Queue depth for this lun */
  1159. u_char scdev_depth; /* SCSI device queue depth */
  1160. u_char maxnxs; /* Max possible nexuses */
  1161. /*----------------------------------------------------------------
  1162. ** Control of tagged command queuing.
  1163. ** Tags allocation is performed using a circular buffer.
  1164. ** This avoids using a loop for tag allocation.
  1165. **----------------------------------------------------------------
  1166. */
  1167. u_char ia_tag; /* Allocation index */
  1168. u_char if_tag; /* Freeing index */
  1169. u_char cb_tags[MAX_TAGS]; /* Circular tags buffer */
  1170. u_char usetags; /* Command queuing is active */
  1171. u_char maxtags; /* Max nr of tags asked by user */
  1172. u_char numtags; /* Current number of tags */
  1173. /*----------------------------------------------------------------
  1174. ** QUEUE FULL control and ORDERED tag control.
  1175. **----------------------------------------------------------------
  1176. */
  1177. /*----------------------------------------------------------------
  1178. ** QUEUE FULL and ORDERED tag control.
  1179. **----------------------------------------------------------------
  1180. */
  1181. u16 num_good; /* Nr of GOOD since QUEUE FULL */
  1182. tagmap_t tags_umap; /* Used tags bitmap */
  1183. tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
  1184. u_long tags_stime; /* Last time we set smap=umap */
  1185. struct ccb * held_ccb; /* CCB held for QUEUE FULL */
  1186. };
  1187. /*========================================================================
  1188. **
  1189. ** Declaration of structs: the launch script.
  1190. **
  1191. **========================================================================
  1192. **
  1193. ** It is part of the CCB and is called by the scripts processor to
  1194. ** start or restart the data structure (nexus).
  1195. ** This 6 DWORDs mini script makes use of prefetching.
  1196. **
  1197. **------------------------------------------------------------------------
  1198. */
  1199. struct launch {
  1200. /*----------------------------------------------------------------
  1201. ** SCR_COPY(4), @(p_phys), @(dsa register)
  1202. ** SCR_JUMP, @(scheduler_point)
  1203. **----------------------------------------------------------------
  1204. */
  1205. ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
  1206. struct link schedule; /* Jump to scheduler point */
  1207. ncrcmd p_phys; /* 'phys' header bus address */
  1208. };
  1209. /*========================================================================
  1210. **
  1211. ** Declaration of structs: global HEADER.
  1212. **
  1213. **========================================================================
  1214. **
  1215. ** This substructure is copied from the ccb to a global address after
  1216. ** selection (or reselection) and copied back before disconnect.
  1217. **
  1218. ** These fields are accessible to the script processor.
  1219. **
  1220. **------------------------------------------------------------------------
  1221. */
  1222. struct head {
  1223. /*----------------------------------------------------------------
  1224. ** Saved data pointer.
  1225. ** Points to the position in the script responsible for the
  1226. ** actual transfer transfer of data.
  1227. ** It's written after reception of a SAVE_DATA_POINTER message.
  1228. ** The goalpointer points after the last transfer command.
  1229. **----------------------------------------------------------------
  1230. */
  1231. u32 savep;
  1232. u32 lastp;
  1233. u32 goalp;
  1234. /*----------------------------------------------------------------
  1235. ** Alternate data pointer.
  1236. ** They are copied back to savep/lastp/goalp by the SCRIPTS
  1237. ** when the direction is unknown and the device claims data out.
  1238. **----------------------------------------------------------------
  1239. */
  1240. u32 wlastp;
  1241. u32 wgoalp;
  1242. /*----------------------------------------------------------------
  1243. ** The virtual address of the ccb containing this header.
  1244. **----------------------------------------------------------------
  1245. */
  1246. struct ccb * cp;
  1247. /*----------------------------------------------------------------
  1248. ** Status fields.
  1249. **----------------------------------------------------------------
  1250. */
  1251. u_char scr_st[4]; /* script status */
  1252. u_char status[4]; /* host status. must be the */
  1253. /* last DWORD of the header. */
  1254. };
  1255. /*
  1256. ** The status bytes are used by the host and the script processor.
  1257. **
  1258. ** The byte corresponding to the host_status must be stored in the
  1259. ** last DWORD of the CCB header since it is used for command
  1260. ** completion (ncr_wakeup()). Doing so, we are sure that the header
  1261. ** has been entirely copied back to the CCB when the host_status is
  1262. ** seen complete by the CPU.
  1263. **
  1264. ** The last four bytes (status[4]) are copied to the scratchb register
  1265. ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
  1266. ** and copied back just after disconnecting.
  1267. ** Inside the script the XX_REG are used.
  1268. **
  1269. ** The first four bytes (scr_st[4]) are used inside the script by
  1270. ** "COPY" commands.
  1271. ** Because source and destination must have the same alignment
  1272. ** in a DWORD, the fields HAVE to be at the choosen offsets.
  1273. ** xerr_st 0 (0x34) scratcha
  1274. ** sync_st 1 (0x05) sxfer
  1275. ** wide_st 3 (0x03) scntl3
  1276. */
  1277. /*
  1278. ** Last four bytes (script)
  1279. */
  1280. #define QU_REG scr0
  1281. #define HS_REG scr1
  1282. #define HS_PRT nc_scr1
  1283. #define SS_REG scr2
  1284. #define SS_PRT nc_scr2
  1285. #define PS_REG scr3
  1286. /*
  1287. ** Last four bytes (host)
  1288. */
  1289. #ifdef SCSI_NCR_BIG_ENDIAN
  1290. #define actualquirks phys.header.status[3]
  1291. #define host_status phys.header.status[2]
  1292. #define scsi_status phys.header.status[1]
  1293. #define parity_status phys.header.status[0]
  1294. #else
  1295. #define actualquirks phys.header.status[0]
  1296. #define host_status phys.header.status[1]
  1297. #define scsi_status phys.header.status[2]
  1298. #define parity_status phys.header.status[3]
  1299. #endif
  1300. /*
  1301. ** First four bytes (script)
  1302. */
  1303. #define xerr_st header.scr_st[0]
  1304. #define sync_st header.scr_st[1]
  1305. #define nego_st header.scr_st[2]
  1306. #define wide_st header.scr_st[3]
  1307. /*
  1308. ** First four bytes (host)
  1309. */
  1310. #define xerr_status phys.xerr_st
  1311. #define nego_status phys.nego_st
  1312. #if 0
  1313. #define sync_status phys.sync_st
  1314. #define wide_status phys.wide_st
  1315. #endif
  1316. /*==========================================================
  1317. **
  1318. ** Declaration of structs: Data structure block
  1319. **
  1320. **==========================================================
  1321. **
  1322. ** During execution of a ccb by the script processor,
  1323. ** the DSA (data structure address) register points
  1324. ** to this substructure of the ccb.
  1325. ** This substructure contains the header with
  1326. ** the script-processor-changable data and
  1327. ** data blocks for the indirect move commands.
  1328. **
  1329. **----------------------------------------------------------
  1330. */
  1331. struct dsb {
  1332. /*
  1333. ** Header.
  1334. */
  1335. struct head header;
  1336. /*
  1337. ** Table data for Script
  1338. */
  1339. struct scr_tblsel select;
  1340. struct scr_tblmove smsg ;
  1341. struct scr_tblmove cmd ;
  1342. struct scr_tblmove sense ;
  1343. struct scr_tblmove data[MAX_SCATTER];
  1344. };
  1345. /*========================================================================
  1346. **
  1347. ** Declaration of structs: Command control block.
  1348. **
  1349. **========================================================================
  1350. */
  1351. struct ccb {
  1352. /*----------------------------------------------------------------
  1353. ** This is the data structure which is pointed by the DSA
  1354. ** register when it is executed by the script processor.
  1355. ** It must be the first entry because it contains the header
  1356. ** as first entry that must be cache line aligned.
  1357. **----------------------------------------------------------------
  1358. */
  1359. struct dsb phys;
  1360. /*----------------------------------------------------------------
  1361. ** Mini-script used at CCB execution start-up.
  1362. ** Load the DSA with the data structure address (phys) and
  1363. ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
  1364. **----------------------------------------------------------------
  1365. */
  1366. struct launch start;
  1367. /*----------------------------------------------------------------
  1368. ** Mini-script used at CCB relection to restart the nexus.
  1369. ** Load the DSA with the data structure address (phys) and
  1370. ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
  1371. **----------------------------------------------------------------
  1372. */
  1373. struct launch restart;
  1374. /*----------------------------------------------------------------
  1375. ** If a data transfer phase is terminated too early
  1376. ** (after reception of a message (i.e. DISCONNECT)),
  1377. ** we have to prepare a mini script to transfer
  1378. ** the rest of the data.
  1379. **----------------------------------------------------------------
  1380. */
  1381. ncrcmd patch[8];
  1382. /*----------------------------------------------------------------
  1383. ** The general SCSI driver provides a
  1384. ** pointer to a control block.
  1385. **----------------------------------------------------------------
  1386. */
  1387. struct scsi_cmnd *cmd; /* SCSI command */
  1388. u_char cdb_buf[16]; /* Copy of CDB */
  1389. u_char sense_buf[64];
  1390. int data_len; /* Total data length */
  1391. /*----------------------------------------------------------------
  1392. ** Message areas.
  1393. ** We prepare a message to be sent after selection.
  1394. ** We may use a second one if the command is rescheduled
  1395. ** due to GETCC or QFULL.
  1396. ** Contents are IDENTIFY and SIMPLE_TAG.
  1397. ** While negotiating sync or wide transfer,
  1398. ** a SDTR or WDTR message is appended.
  1399. **----------------------------------------------------------------
  1400. */
  1401. u_char scsi_smsg [8];
  1402. u_char scsi_smsg2[8];
  1403. /*----------------------------------------------------------------
  1404. ** Other fields.
  1405. **----------------------------------------------------------------
  1406. */
  1407. u_long p_ccb; /* BUS address of this CCB */
  1408. u_char sensecmd[6]; /* Sense command */
  1409. u_char tag; /* Tag for this transfer */
  1410. /* 255 means no tag */
  1411. u_char target;
  1412. u_char lun;
  1413. u_char queued;
  1414. u_char auto_sense;
  1415. struct ccb * link_ccb; /* Host adapter CCB chain */
  1416. struct list_head link_ccbq; /* Link to unit CCB queue */
  1417. u32 startp; /* Initial data pointer */
  1418. u_long magic; /* Free / busy CCB flag */
  1419. };
  1420. #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
  1421. /*========================================================================
  1422. **
  1423. ** Declaration of structs: NCR device descriptor
  1424. **
  1425. **========================================================================
  1426. */
  1427. struct ncb {
  1428. /*----------------------------------------------------------------
  1429. ** The global header.
  1430. ** It is accessible to both the host and the script processor.
  1431. ** Must be cache line size aligned (32 for x86) in order to
  1432. ** allow cache line bursting when it is copied to/from CCB.
  1433. **----------------------------------------------------------------
  1434. */
  1435. struct head header;
  1436. /*----------------------------------------------------------------
  1437. ** CCBs management queues.
  1438. **----------------------------------------------------------------
  1439. */
  1440. struct scsi_cmnd *waiting_list; /* Commands waiting for a CCB */
  1441. /* when lcb is not allocated. */
  1442. struct scsi_cmnd *done_list; /* Commands waiting for done() */
  1443. /* callback to be invoked. */
  1444. spinlock_t smp_lock; /* Lock for SMP threading */
  1445. /*----------------------------------------------------------------
  1446. ** Chip and controller indentification.
  1447. **----------------------------------------------------------------
  1448. */
  1449. int unit; /* Unit number */
  1450. char inst_name[16]; /* ncb instance name */
  1451. /*----------------------------------------------------------------
  1452. ** Initial value of some IO register bits.
  1453. ** These values are assumed to have been set by BIOS, and may
  1454. ** be used for probing adapter implementation differences.
  1455. **----------------------------------------------------------------
  1456. */
  1457. u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest0, sv_ctest3,
  1458. sv_ctest4, sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
  1459. /*----------------------------------------------------------------
  1460. ** Actual initial value of IO register bits used by the
  1461. ** driver. They are loaded at initialisation according to
  1462. ** features that are to be enabled.
  1463. **----------------------------------------------------------------
  1464. */
  1465. u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest0, rv_ctest3,
  1466. rv_ctest4, rv_ctest5, rv_stest2;
  1467. /*----------------------------------------------------------------
  1468. ** Targets management.
  1469. ** During reselection the ncr jumps to jump_tcb.
  1470. ** The SFBR register is loaded with the encoded target id.
  1471. ** For i = 0 to 3
  1472. ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
  1473. **
  1474. ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
  1475. ** It is kind of hashcoding.
  1476. **----------------------------------------------------------------
  1477. */
  1478. struct link jump_tcb[4]; /* JUMPs for reselection */
  1479. struct tcb target[MAX_TARGET]; /* Target data */
  1480. /*----------------------------------------------------------------
  1481. ** Virtual and physical bus addresses of the chip.
  1482. **----------------------------------------------------------------
  1483. */
  1484. void __iomem *vaddr; /* Virtual and bus address of */
  1485. unsigned long paddr; /* chip's IO registers. */
  1486. unsigned long paddr2; /* On-chip RAM bus address. */
  1487. volatile /* Pointer to volatile for */
  1488. struct ncr_reg __iomem *reg; /* memory mapped IO. */
  1489. /*----------------------------------------------------------------
  1490. ** SCRIPTS virtual and physical bus addresses.
  1491. ** 'script' is loaded in the on-chip RAM if present.
  1492. ** 'scripth' stays in main memory.
  1493. **----------------------------------------------------------------
  1494. */
  1495. struct script *script0; /* Copies of script and scripth */
  1496. struct scripth *scripth0; /* relocated for this ncb. */
  1497. struct scripth *scripth; /* Actual scripth virt. address */
  1498. u_long p_script; /* Actual script and scripth */
  1499. u_long p_scripth; /* bus addresses. */
  1500. /*----------------------------------------------------------------
  1501. ** General controller parameters and configuration.
  1502. **----------------------------------------------------------------
  1503. */
  1504. struct device *dev;
  1505. u_char revision_id; /* PCI device revision id */
  1506. u32 irq; /* IRQ level */
  1507. u32 features; /* Chip features map */
  1508. u_char myaddr; /* SCSI id of the adapter */
  1509. u_char maxburst; /* log base 2 of dwords burst */
  1510. u_char maxwide; /* Maximum transfer width */
  1511. u_char minsync; /* Minimum sync period factor */
  1512. u_char maxsync; /* Maximum sync period factor */
  1513. u_char maxoffs; /* Max scsi offset */
  1514. u_char multiplier; /* Clock multiplier (1,2,4) */
  1515. u_char clock_divn; /* Number of clock divisors */
  1516. u_long clock_khz; /* SCSI clock frequency in KHz */
  1517. /*----------------------------------------------------------------
  1518. ** Start queue management.
  1519. ** It is filled up by the host processor and accessed by the
  1520. ** SCRIPTS processor in order to start SCSI commands.
  1521. **----------------------------------------------------------------
  1522. */
  1523. u16 squeueput; /* Next free slot of the queue */
  1524. u16 actccbs; /* Number of allocated CCBs */
  1525. u16 queuedccbs; /* Number of CCBs in start queue*/
  1526. u16 queuedepth; /* Start queue depth */
  1527. /*----------------------------------------------------------------
  1528. ** Timeout handler.
  1529. **----------------------------------------------------------------
  1530. */
  1531. struct timer_list timer; /* Timer handler link header */
  1532. u_long lasttime;
  1533. u_long settle_time; /* Resetting the SCSI BUS */
  1534. /*----------------------------------------------------------------
  1535. ** Debugging and profiling.
  1536. **----------------------------------------------------------------
  1537. */
  1538. struct ncr_reg regdump; /* Register dump */
  1539. u_long regtime; /* Time it has been done */
  1540. /*----------------------------------------------------------------
  1541. ** Miscellaneous buffers accessed by the scripts-processor.
  1542. ** They shall be DWORD aligned, because they may be read or
  1543. ** written with a SCR_COPY script command.
  1544. **----------------------------------------------------------------
  1545. */
  1546. u_char msgout[8]; /* Buffer for MESSAGE OUT */
  1547. u_char msgin [8]; /* Buffer for MESSAGE IN */
  1548. u32 lastmsg; /* Last SCSI message sent */
  1549. u_char scratch; /* Scratch for SCSI receive */
  1550. /*----------------------------------------------------------------
  1551. ** Miscellaneous configuration and status parameters.
  1552. **----------------------------------------------------------------
  1553. */
  1554. u_char disc; /* Diconnection allowed */
  1555. u_char scsi_mode; /* Current SCSI BUS mode */
  1556. u_char order; /* Tag order to use */
  1557. u_char verbose; /* Verbosity for this controller*/
  1558. int ncr_cache; /* Used for cache test at init. */
  1559. u_long p_ncb; /* BUS address of this NCB */
  1560. /*----------------------------------------------------------------
  1561. ** Command completion handling.
  1562. **----------------------------------------------------------------
  1563. */
  1564. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  1565. struct ccb *(ccb_done[MAX_DONE]);
  1566. int ccb_done_ic;
  1567. #endif
  1568. /*----------------------------------------------------------------
  1569. ** Fields that should be removed or changed.
  1570. **----------------------------------------------------------------
  1571. */
  1572. struct ccb *ccb; /* Global CCB */
  1573. struct usrcmd user; /* Command from user */
  1574. volatile u_char release_stage; /* Synchronisation stage on release */
  1575. };
  1576. #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
  1577. #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
  1578. /*==========================================================
  1579. **
  1580. **
  1581. ** Script for NCR-Processor.
  1582. **
  1583. ** Use ncr_script_fill() to create the variable parts.
  1584. ** Use ncr_script_copy_and_bind() to make a copy and
  1585. ** bind to physical addresses.
  1586. **
  1587. **
  1588. **==========================================================
  1589. **
  1590. ** We have to know the offsets of all labels before
  1591. ** we reach them (for forward jumps).
  1592. ** Therefore we declare a struct here.
  1593. ** If you make changes inside the script,
  1594. ** DONT FORGET TO CHANGE THE LENGTHS HERE!
  1595. **
  1596. **----------------------------------------------------------
  1597. */
  1598. /*
  1599. ** For HP Zalon/53c720 systems, the Zalon interface
  1600. ** between CPU and 53c720 does prefetches, which causes
  1601. ** problems with self modifying scripts. The problem
  1602. ** is overcome by calling a dummy subroutine after each
  1603. ** modification, to force a refetch of the script on
  1604. ** return from the subroutine.
  1605. */
  1606. #ifdef CONFIG_NCR53C8XX_PREFETCH
  1607. #define PREFETCH_FLUSH_CNT 2
  1608. #define PREFETCH_FLUSH SCR_CALL, PADDRH (wait_dma),
  1609. #else
  1610. #define PREFETCH_FLUSH_CNT 0
  1611. #define PREFETCH_FLUSH
  1612. #endif
  1613. /*
  1614. ** Script fragments which are loaded into the on-chip RAM
  1615. ** of 825A, 875 and 895 chips.
  1616. */
  1617. struct script {
  1618. ncrcmd start [ 5];
  1619. ncrcmd startpos [ 1];
  1620. ncrcmd select [ 6];
  1621. ncrcmd select2 [ 9 + PREFETCH_FLUSH_CNT];
  1622. ncrcmd loadpos [ 4];
  1623. ncrcmd send_ident [ 9];
  1624. ncrcmd prepare [ 6];
  1625. ncrcmd prepare2 [ 7];
  1626. ncrcmd command [ 6];
  1627. ncrcmd dispatch [ 32];
  1628. ncrcmd clrack [ 4];
  1629. ncrcmd no_data [ 17];
  1630. ncrcmd status [ 8];
  1631. ncrcmd msg_in [ 2];
  1632. ncrcmd msg_in2 [ 16];
  1633. ncrcmd msg_bad [ 4];
  1634. ncrcmd setmsg [ 7];
  1635. ncrcmd cleanup [ 6];
  1636. ncrcmd complete [ 9];
  1637. ncrcmd cleanup_ok [ 8 + PREFETCH_FLUSH_CNT];
  1638. ncrcmd cleanup0 [ 1];
  1639. #ifndef SCSI_NCR_CCB_DONE_SUPPORT
  1640. ncrcmd signal [ 12];
  1641. #else
  1642. ncrcmd signal [ 9];
  1643. ncrcmd done_pos [ 1];
  1644. ncrcmd done_plug [ 2];
  1645. ncrcmd done_end [ 7];
  1646. #endif
  1647. ncrcmd save_dp [ 7];
  1648. ncrcmd restore_dp [ 5];
  1649. ncrcmd disconnect [ 10];
  1650. ncrcmd msg_out [ 9];
  1651. ncrcmd msg_out_done [ 7];
  1652. ncrcmd idle [ 2];
  1653. ncrcmd reselect [ 8];
  1654. ncrcmd reselected [ 8];
  1655. ncrcmd resel_dsa [ 6 + PREFETCH_FLUSH_CNT];
  1656. ncrcmd loadpos1 [ 4];
  1657. ncrcmd resel_lun [ 6];
  1658. ncrcmd resel_tag [ 6];
  1659. ncrcmd jump_to_nexus [ 4 + PREFETCH_FLUSH_CNT];
  1660. ncrcmd nexus_indirect [ 4];
  1661. ncrcmd resel_notag [ 4];
  1662. ncrcmd data_in [MAX_SCATTERL * 4];
  1663. ncrcmd data_in2 [ 4];
  1664. ncrcmd data_out [MAX_SCATTERL * 4];
  1665. ncrcmd data_out2 [ 4];
  1666. };
  1667. /*
  1668. ** Script fragments which stay in main memory for all chips.
  1669. */
  1670. struct scripth {
  1671. ncrcmd tryloop [MAX_START*2];
  1672. ncrcmd tryloop2 [ 2];
  1673. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  1674. ncrcmd done_queue [MAX_DONE*5];
  1675. ncrcmd done_queue2 [ 2];
  1676. #endif
  1677. ncrcmd select_no_atn [ 8];
  1678. ncrcmd cancel [ 4];
  1679. ncrcmd skip [ 9 + PREFETCH_FLUSH_CNT];
  1680. ncrcmd skip2 [ 19];
  1681. ncrcmd par_err_data_in [ 6];
  1682. ncrcmd par_err_other [ 4];
  1683. ncrcmd msg_reject [ 8];
  1684. ncrcmd msg_ign_residue [ 24];
  1685. ncrcmd msg_extended [ 10];
  1686. ncrcmd msg_ext_2 [ 10];
  1687. ncrcmd msg_wdtr [ 14];
  1688. ncrcmd send_wdtr [ 7];
  1689. ncrcmd msg_ext_3 [ 10];
  1690. ncrcmd msg_sdtr [ 14];
  1691. ncrcmd send_sdtr [ 7];
  1692. ncrcmd nego_bad_phase [ 4];
  1693. ncrcmd msg_out_abort [ 10];
  1694. ncrcmd hdata_in [MAX_SCATTERH * 4];
  1695. ncrcmd hdata_in2 [ 2];
  1696. ncrcmd hdata_out [MAX_SCATTERH * 4];
  1697. ncrcmd hdata_out2 [ 2];
  1698. ncrcmd reset [ 4];
  1699. ncrcmd aborttag [ 4];
  1700. ncrcmd abort [ 2];
  1701. ncrcmd abort_resel [ 20];
  1702. ncrcmd resend_ident [ 4];
  1703. ncrcmd clratn_go_on [ 3];
  1704. ncrcmd nxtdsp_go_on [ 1];
  1705. ncrcmd sdata_in [ 8];
  1706. ncrcmd data_io [ 18];
  1707. ncrcmd bad_identify [ 12];
  1708. ncrcmd bad_i_t_l [ 4];
  1709. ncrcmd bad_i_t_l_q [ 4];
  1710. ncrcmd bad_target [ 8];
  1711. ncrcmd bad_status [ 8];
  1712. ncrcmd start_ram [ 4 + PREFETCH_FLUSH_CNT];
  1713. ncrcmd start_ram0 [ 4];
  1714. ncrcmd sto_restart [ 5];
  1715. ncrcmd wait_dma [ 2];
  1716. ncrcmd snooptest [ 9];
  1717. ncrcmd snoopend [ 2];
  1718. };
  1719. /*==========================================================
  1720. **
  1721. **
  1722. ** Function headers.
  1723. **
  1724. **
  1725. **==========================================================
  1726. */
  1727. static void ncr_alloc_ccb (struct ncb *np, u_char tn, u_char ln);
  1728. static void ncr_complete (struct ncb *np, struct ccb *cp);
  1729. static void ncr_exception (struct ncb *np);
  1730. static void ncr_free_ccb (struct ncb *np, struct ccb *cp);
  1731. static void ncr_init_ccb (struct ncb *np, struct ccb *cp);
  1732. static void ncr_init_tcb (struct ncb *np, u_char tn);
  1733. static struct lcb * ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln);
  1734. static struct lcb * ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev);
  1735. static void ncr_getclock (struct ncb *np, int mult);
  1736. static void ncr_selectclock (struct ncb *np, u_char scntl3);
  1737. static struct ccb *ncr_get_ccb (struct ncb *np, struct scsi_cmnd *cmd);
  1738. static void ncr_chip_reset (struct ncb *np, int delay);
  1739. static void ncr_init (struct ncb *np, int reset, char * msg, u_long code);
  1740. static int ncr_int_sbmc (struct ncb *np);
  1741. static int ncr_int_par (struct ncb *np);
  1742. static void ncr_int_ma (struct ncb *np);
  1743. static void ncr_int_sir (struct ncb *np);
  1744. static void ncr_int_sto (struct ncb *np);
  1745. static void ncr_negotiate (struct ncb* np, struct tcb* tp);
  1746. static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr);
  1747. static void ncr_script_copy_and_bind
  1748. (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len);
  1749. static void ncr_script_fill (struct script * scr, struct scripth * scripth);
  1750. static int ncr_scatter (struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd);
  1751. static void ncr_getsync (struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p);
  1752. static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer);
  1753. static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev);
  1754. static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack);
  1755. static int ncr_snooptest (struct ncb *np);
  1756. static void ncr_timeout (struct ncb *np);
  1757. static void ncr_wakeup (struct ncb *np, u_long code);
  1758. static void ncr_wakeup_done (struct ncb *np);
  1759. static void ncr_start_next_ccb (struct ncb *np, struct lcb * lp, int maxn);
  1760. static void ncr_put_start_queue(struct ncb *np, struct ccb *cp);
  1761. static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd);
  1762. static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd);
  1763. static void process_waiting_list(struct ncb *np, int sts);
  1764. #define remove_from_waiting_list(np, cmd) \
  1765. retrieve_from_waiting_list(1, (np), (cmd))
  1766. #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
  1767. #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
  1768. static inline char *ncr_name (struct ncb *np)
  1769. {
  1770. return np->inst_name;
  1771. }
  1772. /*==========================================================
  1773. **
  1774. **
  1775. ** Scripts for NCR-Processor.
  1776. **
  1777. ** Use ncr_script_bind for binding to physical addresses.
  1778. **
  1779. **
  1780. **==========================================================
  1781. **
  1782. ** NADDR generates a reference to a field of the controller data.
  1783. ** PADDR generates a reference to another part of the script.
  1784. ** RADDR generates a reference to a script processor register.
  1785. ** FADDR generates a reference to a script processor register
  1786. ** with offset.
  1787. **
  1788. **----------------------------------------------------------
  1789. */
  1790. #define RELOC_SOFTC 0x40000000
  1791. #define RELOC_LABEL 0x50000000
  1792. #define RELOC_REGISTER 0x60000000
  1793. #if 0
  1794. #define RELOC_KVAR 0x70000000
  1795. #endif
  1796. #define RELOC_LABELH 0x80000000
  1797. #define RELOC_MASK 0xf0000000
  1798. #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
  1799. #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
  1800. #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
  1801. #define RADDR(label) (RELOC_REGISTER | REG(label))
  1802. #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
  1803. #if 0
  1804. #define KVAR(which) (RELOC_KVAR | (which))
  1805. #endif
  1806. #if 0
  1807. #define SCRIPT_KVAR_JIFFIES (0)
  1808. #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
  1809. #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
  1810. /*
  1811. * Kernel variables referenced in the scripts.
  1812. * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
  1813. */
  1814. static void *script_kvars[] __initdata =
  1815. { (void *)&jiffies };
  1816. #endif
  1817. static struct script script0 __initdata = {
  1818. /*--------------------------< START >-----------------------*/ {
  1819. /*
  1820. ** This NOP will be patched with LED ON
  1821. ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
  1822. */
  1823. SCR_NO_OP,
  1824. 0,
  1825. /*
  1826. ** Clear SIGP.
  1827. */
  1828. SCR_FROM_REG (ctest2),
  1829. 0,
  1830. /*
  1831. ** Then jump to a certain point in tryloop.
  1832. ** Due to the lack of indirect addressing the code
  1833. ** is self modifying here.
  1834. */
  1835. SCR_JUMP,
  1836. }/*-------------------------< STARTPOS >--------------------*/,{
  1837. PADDRH(tryloop),
  1838. }/*-------------------------< SELECT >----------------------*/,{
  1839. /*
  1840. ** DSA contains the address of a scheduled
  1841. ** data structure.
  1842. **
  1843. ** SCRATCHA contains the address of the script,
  1844. ** which starts the next entry.
  1845. **
  1846. ** Set Initiator mode.
  1847. **
  1848. ** (Target mode is left as an exercise for the reader)
  1849. */
  1850. SCR_CLR (SCR_TRG),
  1851. 0,
  1852. SCR_LOAD_REG (HS_REG, HS_SELECTING),
  1853. 0,
  1854. /*
  1855. ** And try to select this target.
  1856. */
  1857. SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
  1858. PADDR (reselect),
  1859. }/*-------------------------< SELECT2 >----------------------*/,{
  1860. /*
  1861. ** Now there are 4 possibilities:
  1862. **
  1863. ** (1) The ncr loses arbitration.
  1864. ** This is ok, because it will try again,
  1865. ** when the bus becomes idle.
  1866. ** (But beware of the timeout function!)
  1867. **
  1868. ** (2) The ncr is reselected.
  1869. ** Then the script processor takes the jump
  1870. ** to the RESELECT label.
  1871. **
  1872. ** (3) The ncr wins arbitration.
  1873. ** Then it will execute SCRIPTS instruction until
  1874. ** the next instruction that checks SCSI phase.
  1875. ** Then will stop and wait for selection to be
  1876. ** complete or selection time-out to occur.
  1877. ** As a result the SCRIPTS instructions until
  1878. ** LOADPOS + 2 should be executed in parallel with
  1879. ** the SCSI core performing selection.
  1880. */
  1881. /*
  1882. ** The MESSAGE_REJECT problem seems to be due to a selection
  1883. ** timing problem.
  1884. ** Wait immediately for the selection to complete.
  1885. ** (2.5x behaves so)
  1886. */
  1887. SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  1888. 0,
  1889. /*
  1890. ** Next time use the next slot.
  1891. */
  1892. SCR_COPY (4),
  1893. RADDR (temp),
  1894. PADDR (startpos),
  1895. /*
  1896. ** The ncr doesn't have an indirect load
  1897. ** or store command. So we have to
  1898. ** copy part of the control block to a
  1899. ** fixed place, where we can access it.
  1900. **
  1901. ** We patch the address part of a
  1902. ** COPY command with the DSA-register.
  1903. */
  1904. SCR_COPY_F (4),
  1905. RADDR (dsa),
  1906. PADDR (loadpos),
  1907. /*
  1908. ** Flush script prefetch if required
  1909. */
  1910. PREFETCH_FLUSH
  1911. /*
  1912. ** then we do the actual copy.
  1913. */
  1914. SCR_COPY (sizeof (struct head)),
  1915. /*
  1916. ** continued after the next label ...
  1917. */
  1918. }/*-------------------------< LOADPOS >---------------------*/,{
  1919. 0,
  1920. NADDR (header),
  1921. /*
  1922. ** Wait for the next phase or the selection
  1923. ** to complete or time-out.
  1924. */
  1925. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  1926. PADDR (prepare),
  1927. }/*-------------------------< SEND_IDENT >----------------------*/,{
  1928. /*
  1929. ** Selection complete.
  1930. ** Send the IDENTIFY and SIMPLE_TAG messages
  1931. ** (and the EXTENDED_SDTR message)
  1932. */
  1933. SCR_MOVE_TBL ^ SCR_MSG_OUT,
  1934. offsetof (struct dsb, smsg),
  1935. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
  1936. PADDRH (resend_ident),
  1937. SCR_LOAD_REG (scratcha, 0x80),
  1938. 0,
  1939. SCR_COPY (1),
  1940. RADDR (scratcha),
  1941. NADDR (lastmsg),
  1942. }/*-------------------------< PREPARE >----------------------*/,{
  1943. /*
  1944. ** load the savep (saved pointer) into
  1945. ** the TEMP register (actual pointer)
  1946. */
  1947. SCR_COPY (4),
  1948. NADDR (header.savep),
  1949. RADDR (temp),
  1950. /*
  1951. ** Initialize the status registers
  1952. */
  1953. SCR_COPY (4),
  1954. NADDR (header.status),
  1955. RADDR (scr0),
  1956. }/*-------------------------< PREPARE2 >---------------------*/,{
  1957. /*
  1958. ** Initialize the msgout buffer with a NOOP message.
  1959. */
  1960. SCR_LOAD_REG (scratcha, NOP),
  1961. 0,
  1962. SCR_COPY (1),
  1963. RADDR (scratcha),
  1964. NADDR (msgout),
  1965. #if 0
  1966. SCR_COPY (1),
  1967. RADDR (scratcha),
  1968. NADDR (msgin),
  1969. #endif
  1970. /*
  1971. ** Anticipate the COMMAND phase.
  1972. ** This is the normal case for initial selection.
  1973. */
  1974. SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
  1975. PADDR (dispatch),
  1976. }/*-------------------------< COMMAND >--------------------*/,{
  1977. /*
  1978. ** ... and send the command
  1979. */
  1980. SCR_MOVE_TBL ^ SCR_COMMAND,
  1981. offsetof (struct dsb, cmd),
  1982. /*
  1983. ** If status is still HS_NEGOTIATE, negotiation failed.
  1984. ** We check this here, since we want to do that
  1985. ** only once.
  1986. */
  1987. SCR_FROM_REG (HS_REG),
  1988. 0,
  1989. SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
  1990. SIR_NEGO_FAILED,
  1991. }/*-----------------------< DISPATCH >----------------------*/,{
  1992. /*
  1993. ** MSG_IN is the only phase that shall be
  1994. ** entered at least once for each (re)selection.
  1995. ** So we test it first.
  1996. */
  1997. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
  1998. PADDR (msg_in),
  1999. SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
  2000. 0,
  2001. /*
  2002. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
  2003. ** Possible data corruption during Memory Write and Invalidate.
  2004. ** This work-around resets the addressing logic prior to the
  2005. ** start of the first MOVE of a DATA IN phase.
  2006. ** (See Documentation/scsi/ncr53c8xx.txt for more information)
  2007. */
  2008. SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
  2009. 20,
  2010. SCR_COPY (4),
  2011. RADDR (scratcha),
  2012. RADDR (scratcha),
  2013. SCR_RETURN,
  2014. 0,
  2015. SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
  2016. PADDR (status),
  2017. SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
  2018. PADDR (command),
  2019. SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
  2020. PADDR (msg_out),
  2021. /*
  2022. ** Discard one illegal phase byte, if required.
  2023. */
  2024. SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
  2025. 0,
  2026. SCR_COPY (1),
  2027. RADDR (scratcha),
  2028. NADDR (xerr_st),
  2029. SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
  2030. 8,
  2031. SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
  2032. NADDR (scratch),
  2033. SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
  2034. 8,
  2035. SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
  2036. NADDR (scratch),
  2037. SCR_JUMP,
  2038. PADDR (dispatch),
  2039. }/*-------------------------< CLRACK >----------------------*/,{
  2040. /*
  2041. ** Terminate possible pending message phase.
  2042. */
  2043. SCR_CLR (SCR_ACK),
  2044. 0,
  2045. SCR_JUMP,
  2046. PADDR (dispatch),
  2047. }/*-------------------------< NO_DATA >--------------------*/,{
  2048. /*
  2049. ** The target wants to tranfer too much data
  2050. ** or in the wrong direction.
  2051. ** Remember that in extended error.
  2052. */
  2053. SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
  2054. 0,
  2055. SCR_COPY (1),
  2056. RADDR (scratcha),
  2057. NADDR (xerr_st),
  2058. /*
  2059. ** Discard one data byte, if required.
  2060. */
  2061. SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2062. 8,
  2063. SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
  2064. NADDR (scratch),
  2065. SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
  2066. 8,
  2067. SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
  2068. NADDR (scratch),
  2069. /*
  2070. ** .. and repeat as required.
  2071. */
  2072. SCR_CALL,
  2073. PADDR (dispatch),
  2074. SCR_JUMP,
  2075. PADDR (no_data),
  2076. }/*-------------------------< STATUS >--------------------*/,{
  2077. /*
  2078. ** get the status
  2079. */
  2080. SCR_MOVE_ABS (1) ^ SCR_STATUS,
  2081. NADDR (scratch),
  2082. /*
  2083. ** save status to scsi_status.
  2084. ** mark as complete.
  2085. */
  2086. SCR_TO_REG (SS_REG),
  2087. 0,
  2088. SCR_LOAD_REG (HS_REG, HS_COMPLETE),
  2089. 0,
  2090. SCR_JUMP,
  2091. PADDR (dispatch),
  2092. }/*-------------------------< MSG_IN >--------------------*/,{
  2093. /*
  2094. ** Get the first byte of the message
  2095. ** and save it to SCRATCHA.
  2096. **
  2097. ** The script processor doesn't negate the
  2098. ** ACK signal after this transfer.
  2099. */
  2100. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2101. NADDR (msgin[0]),
  2102. }/*-------------------------< MSG_IN2 >--------------------*/,{
  2103. /*
  2104. ** Handle this message.
  2105. */
  2106. SCR_JUMP ^ IFTRUE (DATA (COMMAND_COMPLETE)),
  2107. PADDR (complete),
  2108. SCR_JUMP ^ IFTRUE (DATA (DISCONNECT)),
  2109. PADDR (disconnect),
  2110. SCR_JUMP ^ IFTRUE (DATA (SAVE_POINTERS)),
  2111. PADDR (save_dp),
  2112. SCR_JUMP ^ IFTRUE (DATA (RESTORE_POINTERS)),
  2113. PADDR (restore_dp),
  2114. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_MESSAGE)),
  2115. PADDRH (msg_extended),
  2116. SCR_JUMP ^ IFTRUE (DATA (NOP)),
  2117. PADDR (clrack),
  2118. SCR_JUMP ^ IFTRUE (DATA (MESSAGE_REJECT)),
  2119. PADDRH (msg_reject),
  2120. SCR_JUMP ^ IFTRUE (DATA (IGNORE_WIDE_RESIDUE)),
  2121. PADDRH (msg_ign_residue),
  2122. /*
  2123. ** Rest of the messages left as
  2124. ** an exercise ...
  2125. **
  2126. ** Unimplemented messages:
  2127. ** fall through to MSG_BAD.
  2128. */
  2129. }/*-------------------------< MSG_BAD >------------------*/,{
  2130. /*
  2131. ** unimplemented message - reject it.
  2132. */
  2133. SCR_INT,
  2134. SIR_REJECT_SENT,
  2135. SCR_LOAD_REG (scratcha, MESSAGE_REJECT),
  2136. 0,
  2137. }/*-------------------------< SETMSG >----------------------*/,{
  2138. SCR_COPY (1),
  2139. RADDR (scratcha),
  2140. NADDR (msgout),
  2141. SCR_SET (SCR_ATN),
  2142. 0,
  2143. SCR_JUMP,
  2144. PADDR (clrack),
  2145. }/*-------------------------< CLEANUP >-------------------*/,{
  2146. /*
  2147. ** dsa: Pointer to ccb
  2148. ** or xxxxxxFF (no ccb)
  2149. **
  2150. ** HS_REG: Host-Status (<>0!)
  2151. */
  2152. SCR_FROM_REG (dsa),
  2153. 0,
  2154. SCR_JUMP ^ IFTRUE (DATA (0xff)),
  2155. PADDR (start),
  2156. /*
  2157. ** dsa is valid.
  2158. ** complete the cleanup.
  2159. */
  2160. SCR_JUMP,
  2161. PADDR (cleanup_ok),
  2162. }/*-------------------------< COMPLETE >-----------------*/,{
  2163. /*
  2164. ** Complete message.
  2165. **
  2166. ** Copy TEMP register to LASTP in header.
  2167. */
  2168. SCR_COPY (4),
  2169. RADDR (temp),
  2170. NADDR (header.lastp),
  2171. /*
  2172. ** When we terminate the cycle by clearing ACK,
  2173. ** the target may disconnect immediately.
  2174. **
  2175. ** We don't want to be told of an
  2176. ** "unexpected disconnect",
  2177. ** so we disable this feature.
  2178. */
  2179. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2180. 0,
  2181. /*
  2182. ** Terminate cycle ...
  2183. */
  2184. SCR_CLR (SCR_ACK|SCR_ATN),
  2185. 0,
  2186. /*
  2187. ** ... and wait for the disconnect.
  2188. */
  2189. SCR_WAIT_DISC,
  2190. 0,
  2191. }/*-------------------------< CLEANUP_OK >----------------*/,{
  2192. /*
  2193. ** Save host status to header.
  2194. */
  2195. SCR_COPY (4),
  2196. RADDR (scr0),
  2197. NADDR (header.status),
  2198. /*
  2199. ** and copy back the header to the ccb.
  2200. */
  2201. SCR_COPY_F (4),
  2202. RADDR (dsa),
  2203. PADDR (cleanup0),
  2204. /*
  2205. ** Flush script prefetch if required
  2206. */
  2207. PREFETCH_FLUSH
  2208. SCR_COPY (sizeof (struct head)),
  2209. NADDR (header),
  2210. }/*-------------------------< CLEANUP0 >--------------------*/,{
  2211. 0,
  2212. }/*-------------------------< SIGNAL >----------------------*/,{
  2213. /*
  2214. ** if job not completed ...
  2215. */
  2216. SCR_FROM_REG (HS_REG),
  2217. 0,
  2218. /*
  2219. ** ... start the next command.
  2220. */
  2221. SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
  2222. PADDR(start),
  2223. /*
  2224. ** If command resulted in not GOOD status,
  2225. ** call the C code if needed.
  2226. */
  2227. SCR_FROM_REG (SS_REG),
  2228. 0,
  2229. SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
  2230. PADDRH (bad_status),
  2231. #ifndef SCSI_NCR_CCB_DONE_SUPPORT
  2232. /*
  2233. ** ... signal completion to the host
  2234. */
  2235. SCR_INT,
  2236. SIR_INTFLY,
  2237. /*
  2238. ** Auf zu neuen Schandtaten!
  2239. */
  2240. SCR_JUMP,
  2241. PADDR(start),
  2242. #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
  2243. /*
  2244. ** ... signal completion to the host
  2245. */
  2246. SCR_JUMP,
  2247. }/*------------------------< DONE_POS >---------------------*/,{
  2248. PADDRH (done_queue),
  2249. }/*------------------------< DONE_PLUG >--------------------*/,{
  2250. SCR_INT,
  2251. SIR_DONE_OVERFLOW,
  2252. }/*------------------------< DONE_END >---------------------*/,{
  2253. SCR_INT,
  2254. SIR_INTFLY,
  2255. SCR_COPY (4),
  2256. RADDR (temp),
  2257. PADDR (done_pos),
  2258. SCR_JUMP,
  2259. PADDR (start),
  2260. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  2261. }/*-------------------------< SAVE_DP >------------------*/,{
  2262. /*
  2263. ** SAVE_DP message:
  2264. ** Copy TEMP register to SAVEP in header.
  2265. */
  2266. SCR_COPY (4),
  2267. RADDR (temp),
  2268. NADDR (header.savep),
  2269. SCR_CLR (SCR_ACK),
  2270. 0,
  2271. SCR_JUMP,
  2272. PADDR (dispatch),
  2273. }/*-------------------------< RESTORE_DP >---------------*/,{
  2274. /*
  2275. ** RESTORE_DP message:
  2276. ** Copy SAVEP in header to TEMP register.
  2277. */
  2278. SCR_COPY (4),
  2279. NADDR (header.savep),
  2280. RADDR (temp),
  2281. SCR_JUMP,
  2282. PADDR (clrack),
  2283. }/*-------------------------< DISCONNECT >---------------*/,{
  2284. /*
  2285. ** DISCONNECTing ...
  2286. **
  2287. ** disable the "unexpected disconnect" feature,
  2288. ** and remove the ACK signal.
  2289. */
  2290. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2291. 0,
  2292. SCR_CLR (SCR_ACK|SCR_ATN),
  2293. 0,
  2294. /*
  2295. ** Wait for the disconnect.
  2296. */
  2297. SCR_WAIT_DISC,
  2298. 0,
  2299. /*
  2300. ** Status is: DISCONNECTED.
  2301. */
  2302. SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
  2303. 0,
  2304. SCR_JUMP,
  2305. PADDR (cleanup_ok),
  2306. }/*-------------------------< MSG_OUT >-------------------*/,{
  2307. /*
  2308. ** The target requests a message.
  2309. */
  2310. SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
  2311. NADDR (msgout),
  2312. SCR_COPY (1),
  2313. NADDR (msgout),
  2314. NADDR (lastmsg),
  2315. /*
  2316. ** If it was no ABORT message ...
  2317. */
  2318. SCR_JUMP ^ IFTRUE (DATA (ABORT_TASK_SET)),
  2319. PADDRH (msg_out_abort),
  2320. /*
  2321. ** ... wait for the next phase
  2322. ** if it's a message out, send it again, ...
  2323. */
  2324. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
  2325. PADDR (msg_out),
  2326. }/*-------------------------< MSG_OUT_DONE >--------------*/,{
  2327. /*
  2328. ** ... else clear the message ...
  2329. */
  2330. SCR_LOAD_REG (scratcha, NOP),
  2331. 0,
  2332. SCR_COPY (4),
  2333. RADDR (scratcha),
  2334. NADDR (msgout),
  2335. /*
  2336. ** ... and process the next phase
  2337. */
  2338. SCR_JUMP,
  2339. PADDR (dispatch),
  2340. }/*-------------------------< IDLE >------------------------*/,{
  2341. /*
  2342. ** Nothing to do?
  2343. ** Wait for reselect.
  2344. ** This NOP will be patched with LED OFF
  2345. ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
  2346. */
  2347. SCR_NO_OP,
  2348. 0,
  2349. }/*-------------------------< RESELECT >--------------------*/,{
  2350. /*
  2351. ** make the DSA invalid.
  2352. */
  2353. SCR_LOAD_REG (dsa, 0xff),
  2354. 0,
  2355. SCR_CLR (SCR_TRG),
  2356. 0,
  2357. SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
  2358. 0,
  2359. /*
  2360. ** Sleep waiting for a reselection.
  2361. ** If SIGP is set, special treatment.
  2362. **
  2363. ** Zu allem bereit ..
  2364. */
  2365. SCR_WAIT_RESEL,
  2366. PADDR(start),
  2367. }/*-------------------------< RESELECTED >------------------*/,{
  2368. /*
  2369. ** This NOP will be patched with LED ON
  2370. ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
  2371. */
  2372. SCR_NO_OP,
  2373. 0,
  2374. /*
  2375. ** ... zu nichts zu gebrauchen ?
  2376. **
  2377. ** load the target id into the SFBR
  2378. ** and jump to the control block.
  2379. **
  2380. ** Look at the declarations of
  2381. ** - struct ncb
  2382. ** - struct tcb
  2383. ** - struct lcb
  2384. ** - struct ccb
  2385. ** to understand what's going on.
  2386. */
  2387. SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
  2388. 0,
  2389. SCR_TO_REG (sdid),
  2390. 0,
  2391. SCR_JUMP,
  2392. NADDR (jump_tcb),
  2393. }/*-------------------------< RESEL_DSA >-------------------*/,{
  2394. /*
  2395. ** Ack the IDENTIFY or TAG previously received.
  2396. */
  2397. SCR_CLR (SCR_ACK),
  2398. 0,
  2399. /*
  2400. ** The ncr doesn't have an indirect load
  2401. ** or store command. So we have to
  2402. ** copy part of the control block to a
  2403. ** fixed place, where we can access it.
  2404. **
  2405. ** We patch the address part of a
  2406. ** COPY command with the DSA-register.
  2407. */
  2408. SCR_COPY_F (4),
  2409. RADDR (dsa),
  2410. PADDR (loadpos1),
  2411. /*
  2412. ** Flush script prefetch if required
  2413. */
  2414. PREFETCH_FLUSH
  2415. /*
  2416. ** then we do the actual copy.
  2417. */
  2418. SCR_COPY (sizeof (struct head)),
  2419. /*
  2420. ** continued after the next label ...
  2421. */
  2422. }/*-------------------------< LOADPOS1 >-------------------*/,{
  2423. 0,
  2424. NADDR (header),
  2425. /*
  2426. ** The DSA contains the data structure address.
  2427. */
  2428. SCR_JUMP,
  2429. PADDR (prepare),
  2430. }/*-------------------------< RESEL_LUN >-------------------*/,{
  2431. /*
  2432. ** come back to this point
  2433. ** to get an IDENTIFY message
  2434. ** Wait for a msg_in phase.
  2435. */
  2436. SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2437. SIR_RESEL_NO_MSG_IN,
  2438. /*
  2439. ** message phase.
  2440. ** Read the data directly from the BUS DATA lines.
  2441. ** This helps to support very old SCSI devices that
  2442. ** may reselect without sending an IDENTIFY.
  2443. */
  2444. SCR_FROM_REG (sbdl),
  2445. 0,
  2446. /*
  2447. ** It should be an Identify message.
  2448. */
  2449. SCR_RETURN,
  2450. 0,
  2451. }/*-------------------------< RESEL_TAG >-------------------*/,{
  2452. /*
  2453. ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
  2454. ** Agressive optimization, is'nt it?
  2455. ** No need to test the SIMPLE TAG message, since the
  2456. ** driver only supports conformant devices for tags. ;-)
  2457. */
  2458. SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
  2459. NADDR (msgin),
  2460. /*
  2461. ** Read the TAG from the SIDL.
  2462. ** Still an aggressive optimization. ;-)
  2463. ** Compute the CCB indirect jump address which
  2464. ** is (#TAG*2 & 0xfc) due to tag numbering using
  2465. ** 1,3,5..MAXTAGS*2+1 actual values.
  2466. */
  2467. SCR_REG_SFBR (sidl, SCR_SHL, 0),
  2468. 0,
  2469. SCR_SFBR_REG (temp, SCR_AND, 0xfc),
  2470. 0,
  2471. }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
  2472. SCR_COPY_F (4),
  2473. RADDR (temp),
  2474. PADDR (nexus_indirect),
  2475. /*
  2476. ** Flush script prefetch if required
  2477. */
  2478. PREFETCH_FLUSH
  2479. SCR_COPY (4),
  2480. }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
  2481. 0,
  2482. RADDR (temp),
  2483. SCR_RETURN,
  2484. 0,
  2485. }/*-------------------------< RESEL_NOTAG >-------------------*/,{
  2486. /*
  2487. ** No tag expected.
  2488. ** Read an throw away the IDENTIFY.
  2489. */
  2490. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2491. NADDR (msgin),
  2492. SCR_JUMP,
  2493. PADDR (jump_to_nexus),
  2494. }/*-------------------------< DATA_IN >--------------------*/,{
  2495. /*
  2496. ** Because the size depends on the
  2497. ** #define MAX_SCATTERL parameter,
  2498. ** it is filled in at runtime.
  2499. **
  2500. ** ##===========< i=0; i<MAX_SCATTERL >=========
  2501. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2502. ** || PADDR (dispatch),
  2503. ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
  2504. ** || offsetof (struct dsb, data[ i]),
  2505. ** ##==========================================
  2506. **
  2507. **---------------------------------------------------------
  2508. */
  2509. 0
  2510. }/*-------------------------< DATA_IN2 >-------------------*/,{
  2511. SCR_CALL,
  2512. PADDR (dispatch),
  2513. SCR_JUMP,
  2514. PADDR (no_data),
  2515. }/*-------------------------< DATA_OUT >--------------------*/,{
  2516. /*
  2517. ** Because the size depends on the
  2518. ** #define MAX_SCATTERL parameter,
  2519. ** it is filled in at runtime.
  2520. **
  2521. ** ##===========< i=0; i<MAX_SCATTERL >=========
  2522. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2523. ** || PADDR (dispatch),
  2524. ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
  2525. ** || offsetof (struct dsb, data[ i]),
  2526. ** ##==========================================
  2527. **
  2528. **---------------------------------------------------------
  2529. */
  2530. 0
  2531. }/*-------------------------< DATA_OUT2 >-------------------*/,{
  2532. SCR_CALL,
  2533. PADDR (dispatch),
  2534. SCR_JUMP,
  2535. PADDR (no_data),
  2536. }/*--------------------------------------------------------*/
  2537. };
  2538. static struct scripth scripth0 __initdata = {
  2539. /*-------------------------< TRYLOOP >---------------------*/{
  2540. /*
  2541. ** Start the next entry.
  2542. ** Called addresses point to the launch script in the CCB.
  2543. ** They are patched by the main processor.
  2544. **
  2545. ** Because the size depends on the
  2546. ** #define MAX_START parameter, it is filled
  2547. ** in at runtime.
  2548. **
  2549. **-----------------------------------------------------------
  2550. **
  2551. ** ##===========< I=0; i<MAX_START >===========
  2552. ** || SCR_CALL,
  2553. ** || PADDR (idle),
  2554. ** ##==========================================
  2555. **
  2556. **-----------------------------------------------------------
  2557. */
  2558. 0
  2559. }/*------------------------< TRYLOOP2 >---------------------*/,{
  2560. SCR_JUMP,
  2561. PADDRH(tryloop),
  2562. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  2563. }/*------------------------< DONE_QUEUE >-------------------*/,{
  2564. /*
  2565. ** Copy the CCB address to the next done entry.
  2566. ** Because the size depends on the
  2567. ** #define MAX_DONE parameter, it is filled
  2568. ** in at runtime.
  2569. **
  2570. **-----------------------------------------------------------
  2571. **
  2572. ** ##===========< I=0; i<MAX_DONE >===========
  2573. ** || SCR_COPY (sizeof(struct ccb *),
  2574. ** || NADDR (header.cp),
  2575. ** || NADDR (ccb_done[i]),
  2576. ** || SCR_CALL,
  2577. ** || PADDR (done_end),
  2578. ** ##==========================================
  2579. **
  2580. **-----------------------------------------------------------
  2581. */
  2582. 0
  2583. }/*------------------------< DONE_QUEUE2 >------------------*/,{
  2584. SCR_JUMP,
  2585. PADDRH (done_queue),
  2586. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  2587. }/*------------------------< SELECT_NO_ATN >-----------------*/,{
  2588. /*
  2589. ** Set Initiator mode.
  2590. ** And try to select this target without ATN.
  2591. */
  2592. SCR_CLR (SCR_TRG),
  2593. 0,
  2594. SCR_LOAD_REG (HS_REG, HS_SELECTING),
  2595. 0,
  2596. SCR_SEL_TBL ^ offsetof (struct dsb, select),
  2597. PADDR (reselect),
  2598. SCR_JUMP,
  2599. PADDR (select2),
  2600. }/*-------------------------< CANCEL >------------------------*/,{
  2601. SCR_LOAD_REG (scratcha, HS_ABORTED),
  2602. 0,
  2603. SCR_JUMPR,
  2604. 8,
  2605. }/*-------------------------< SKIP >------------------------*/,{
  2606. SCR_LOAD_REG (scratcha, 0),
  2607. 0,
  2608. /*
  2609. ** This entry has been canceled.
  2610. ** Next time use the next slot.
  2611. */
  2612. SCR_COPY (4),
  2613. RADDR (temp),
  2614. PADDR (startpos),
  2615. /*
  2616. ** The ncr doesn't have an indirect load
  2617. ** or store command. So we have to
  2618. ** copy part of the control block to a
  2619. ** fixed place, where we can access it.
  2620. **
  2621. ** We patch the address part of a
  2622. ** COPY command with the DSA-register.
  2623. */
  2624. SCR_COPY_F (4),
  2625. RADDR (dsa),
  2626. PADDRH (skip2),
  2627. /*
  2628. ** Flush script prefetch if required
  2629. */
  2630. PREFETCH_FLUSH
  2631. /*
  2632. ** then we do the actual copy.
  2633. */
  2634. SCR_COPY (sizeof (struct head)),
  2635. /*
  2636. ** continued after the next label ...
  2637. */
  2638. }/*-------------------------< SKIP2 >---------------------*/,{
  2639. 0,
  2640. NADDR (header),
  2641. /*
  2642. ** Initialize the status registers
  2643. */
  2644. SCR_COPY (4),
  2645. NADDR (header.status),
  2646. RADDR (scr0),
  2647. /*
  2648. ** Force host status.
  2649. */
  2650. SCR_FROM_REG (scratcha),
  2651. 0,
  2652. SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
  2653. 16,
  2654. SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
  2655. 0,
  2656. SCR_JUMPR,
  2657. 8,
  2658. SCR_TO_REG (HS_REG),
  2659. 0,
  2660. SCR_LOAD_REG (SS_REG, S_GOOD),
  2661. 0,
  2662. SCR_JUMP,
  2663. PADDR (cleanup_ok),
  2664. },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
  2665. /*
  2666. ** Ignore all data in byte, until next phase
  2667. */
  2668. SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2669. PADDRH (par_err_other),
  2670. SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
  2671. NADDR (scratch),
  2672. SCR_JUMPR,
  2673. -24,
  2674. },/*-------------------------< PAR_ERR_OTHER >------------------*/{
  2675. /*
  2676. ** count it.
  2677. */
  2678. SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
  2679. 0,
  2680. /*
  2681. ** jump to dispatcher.
  2682. */
  2683. SCR_JUMP,
  2684. PADDR (dispatch),
  2685. }/*-------------------------< MSG_REJECT >---------------*/,{
  2686. /*
  2687. ** If a negotiation was in progress,
  2688. ** negotiation failed.
  2689. ** Otherwise, let the C code print
  2690. ** some message.
  2691. */
  2692. SCR_FROM_REG (HS_REG),
  2693. 0,
  2694. SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
  2695. SIR_REJECT_RECEIVED,
  2696. SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
  2697. SIR_NEGO_FAILED,
  2698. SCR_JUMP,
  2699. PADDR (clrack),
  2700. }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
  2701. /*
  2702. ** Terminate cycle
  2703. */
  2704. SCR_CLR (SCR_ACK),
  2705. 0,
  2706. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2707. PADDR (dispatch),
  2708. /*
  2709. ** get residue size.
  2710. */
  2711. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2712. NADDR (msgin[1]),
  2713. /*
  2714. ** Size is 0 .. ignore message.
  2715. */
  2716. SCR_JUMP ^ IFTRUE (DATA (0)),
  2717. PADDR (clrack),
  2718. /*
  2719. ** Size is not 1 .. have to interrupt.
  2720. */
  2721. SCR_JUMPR ^ IFFALSE (DATA (1)),
  2722. 40,
  2723. /*
  2724. ** Check for residue byte in swide register
  2725. */
  2726. SCR_FROM_REG (scntl2),
  2727. 0,
  2728. SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
  2729. 16,
  2730. /*
  2731. ** There IS data in the swide register.
  2732. ** Discard it.
  2733. */
  2734. SCR_REG_REG (scntl2, SCR_OR, WSR),
  2735. 0,
  2736. SCR_JUMP,
  2737. PADDR (clrack),
  2738. /*
  2739. ** Load again the size to the sfbr register.
  2740. */
  2741. SCR_FROM_REG (scratcha),
  2742. 0,
  2743. SCR_INT,
  2744. SIR_IGN_RESIDUE,
  2745. SCR_JUMP,
  2746. PADDR (clrack),
  2747. }/*-------------------------< MSG_EXTENDED >-------------*/,{
  2748. /*
  2749. ** Terminate cycle
  2750. */
  2751. SCR_CLR (SCR_ACK),
  2752. 0,
  2753. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2754. PADDR (dispatch),
  2755. /*
  2756. ** get length.
  2757. */
  2758. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2759. NADDR (msgin[1]),
  2760. /*
  2761. */
  2762. SCR_JUMP ^ IFTRUE (DATA (3)),
  2763. PADDRH (msg_ext_3),
  2764. SCR_JUMP ^ IFFALSE (DATA (2)),
  2765. PADDR (msg_bad),
  2766. }/*-------------------------< MSG_EXT_2 >----------------*/,{
  2767. SCR_CLR (SCR_ACK),
  2768. 0,
  2769. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2770. PADDR (dispatch),
  2771. /*
  2772. ** get extended message code.
  2773. */
  2774. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2775. NADDR (msgin[2]),
  2776. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_WDTR)),
  2777. PADDRH (msg_wdtr),
  2778. /*
  2779. ** unknown extended message
  2780. */
  2781. SCR_JUMP,
  2782. PADDR (msg_bad)
  2783. }/*-------------------------< MSG_WDTR >-----------------*/,{
  2784. SCR_CLR (SCR_ACK),
  2785. 0,
  2786. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2787. PADDR (dispatch),
  2788. /*
  2789. ** get data bus width
  2790. */
  2791. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2792. NADDR (msgin[3]),
  2793. /*
  2794. ** let the host do the real work.
  2795. */
  2796. SCR_INT,
  2797. SIR_NEGO_WIDE,
  2798. /*
  2799. ** let the target fetch our answer.
  2800. */
  2801. SCR_SET (SCR_ATN),
  2802. 0,
  2803. SCR_CLR (SCR_ACK),
  2804. 0,
  2805. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  2806. PADDRH (nego_bad_phase),
  2807. }/*-------------------------< SEND_WDTR >----------------*/,{
  2808. /*
  2809. ** Send the EXTENDED_WDTR
  2810. */
  2811. SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
  2812. NADDR (msgout),
  2813. SCR_COPY (1),
  2814. NADDR (msgout),
  2815. NADDR (lastmsg),
  2816. SCR_JUMP,
  2817. PADDR (msg_out_done),
  2818. }/*-------------------------< MSG_EXT_3 >----------------*/,{
  2819. SCR_CLR (SCR_ACK),
  2820. 0,
  2821. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2822. PADDR (dispatch),
  2823. /*
  2824. ** get extended message code.
  2825. */
  2826. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2827. NADDR (msgin[2]),
  2828. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_SDTR)),
  2829. PADDRH (msg_sdtr),
  2830. /*
  2831. ** unknown extended message
  2832. */
  2833. SCR_JUMP,
  2834. PADDR (msg_bad)
  2835. }/*-------------------------< MSG_SDTR >-----------------*/,{
  2836. SCR_CLR (SCR_ACK),
  2837. 0,
  2838. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2839. PADDR (dispatch),
  2840. /*
  2841. ** get period and offset
  2842. */
  2843. SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
  2844. NADDR (msgin[3]),
  2845. /*
  2846. ** let the host do the real work.
  2847. */
  2848. SCR_INT,
  2849. SIR_NEGO_SYNC,
  2850. /*
  2851. ** let the target fetch our answer.
  2852. */
  2853. SCR_SET (SCR_ATN),
  2854. 0,
  2855. SCR_CLR (SCR_ACK),
  2856. 0,
  2857. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  2858. PADDRH (nego_bad_phase),
  2859. }/*-------------------------< SEND_SDTR >-------------*/,{
  2860. /*
  2861. ** Send the EXTENDED_SDTR
  2862. */
  2863. SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
  2864. NADDR (msgout),
  2865. SCR_COPY (1),
  2866. NADDR (msgout),
  2867. NADDR (lastmsg),
  2868. SCR_JUMP,
  2869. PADDR (msg_out_done),
  2870. }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
  2871. SCR_INT,
  2872. SIR_NEGO_PROTO,
  2873. SCR_JUMP,
  2874. PADDR (dispatch),
  2875. }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
  2876. /*
  2877. ** After ABORT message,
  2878. **
  2879. ** expect an immediate disconnect, ...
  2880. */
  2881. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2882. 0,
  2883. SCR_CLR (SCR_ACK|SCR_ATN),
  2884. 0,
  2885. SCR_WAIT_DISC,
  2886. 0,
  2887. /*
  2888. ** ... and set the status to "ABORTED"
  2889. */
  2890. SCR_LOAD_REG (HS_REG, HS_ABORTED),
  2891. 0,
  2892. SCR_JUMP,
  2893. PADDR (cleanup),
  2894. }/*-------------------------< HDATA_IN >-------------------*/,{
  2895. /*
  2896. ** Because the size depends on the
  2897. ** #define MAX_SCATTERH parameter,
  2898. ** it is filled in at runtime.
  2899. **
  2900. ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
  2901. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2902. ** || PADDR (dispatch),
  2903. ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
  2904. ** || offsetof (struct dsb, data[ i]),
  2905. ** ##===================================================
  2906. **
  2907. **---------------------------------------------------------
  2908. */
  2909. 0
  2910. }/*-------------------------< HDATA_IN2 >------------------*/,{
  2911. SCR_JUMP,
  2912. PADDR (data_in),
  2913. }/*-------------------------< HDATA_OUT >-------------------*/,{
  2914. /*
  2915. ** Because the size depends on the
  2916. ** #define MAX_SCATTERH parameter,
  2917. ** it is filled in at runtime.
  2918. **
  2919. ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
  2920. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2921. ** || PADDR (dispatch),
  2922. ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
  2923. ** || offsetof (struct dsb, data[ i]),
  2924. ** ##===================================================
  2925. **
  2926. **---------------------------------------------------------
  2927. */
  2928. 0
  2929. }/*-------------------------< HDATA_OUT2 >------------------*/,{
  2930. SCR_JUMP,
  2931. PADDR (data_out),
  2932. }/*-------------------------< RESET >----------------------*/,{
  2933. /*
  2934. ** Send a TARGET_RESET message if bad IDENTIFY
  2935. ** received on reselection.
  2936. */
  2937. SCR_LOAD_REG (scratcha, ABORT_TASK),
  2938. 0,
  2939. SCR_JUMP,
  2940. PADDRH (abort_resel),
  2941. }/*-------------------------< ABORTTAG >-------------------*/,{
  2942. /*
  2943. ** Abort a wrong tag received on reselection.
  2944. */
  2945. SCR_LOAD_REG (scratcha, ABORT_TASK),
  2946. 0,
  2947. SCR_JUMP,
  2948. PADDRH (abort_resel),
  2949. }/*-------------------------< ABORT >----------------------*/,{
  2950. /*
  2951. ** Abort a reselection when no active CCB.
  2952. */
  2953. SCR_LOAD_REG (scratcha, ABORT_TASK_SET),
  2954. 0,
  2955. }/*-------------------------< ABORT_RESEL >----------------*/,{
  2956. SCR_COPY (1),
  2957. RADDR (scratcha),
  2958. NADDR (msgout),
  2959. SCR_SET (SCR_ATN),
  2960. 0,
  2961. SCR_CLR (SCR_ACK),
  2962. 0,
  2963. /*
  2964. ** and send it.
  2965. ** we expect an immediate disconnect
  2966. */
  2967. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2968. 0,
  2969. SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
  2970. NADDR (msgout),
  2971. SCR_COPY (1),
  2972. NADDR (msgout),
  2973. NADDR (lastmsg),
  2974. SCR_CLR (SCR_ACK|SCR_ATN),
  2975. 0,
  2976. SCR_WAIT_DISC,
  2977. 0,
  2978. SCR_JUMP,
  2979. PADDR (start),
  2980. }/*-------------------------< RESEND_IDENT >-------------------*/,{
  2981. /*
  2982. ** The target stays in MSG OUT phase after having acked
  2983. ** Identify [+ Tag [+ Extended message ]]. Targets shall
  2984. ** behave this way on parity error.
  2985. ** We must send it again all the messages.
  2986. */
  2987. SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
  2988. 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
  2989. SCR_JUMP,
  2990. PADDR (send_ident),
  2991. }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
  2992. SCR_CLR (SCR_ATN),
  2993. 0,
  2994. SCR_JUMP,
  2995. }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
  2996. 0,
  2997. }/*-------------------------< SDATA_IN >-------------------*/,{
  2998. SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2999. PADDR (dispatch),
  3000. SCR_MOVE_TBL ^ SCR_DATA_IN,
  3001. offsetof (struct dsb, sense),
  3002. SCR_CALL,
  3003. PADDR (dispatch),
  3004. SCR_JUMP,
  3005. PADDR (no_data),
  3006. }/*-------------------------< DATA_IO >--------------------*/,{
  3007. /*
  3008. ** We jump here if the data direction was unknown at the
  3009. ** time we had to queue the command to the scripts processor.
  3010. ** Pointers had been set as follow in this situation:
  3011. ** savep --> DATA_IO
  3012. ** lastp --> start pointer when DATA_IN
  3013. ** goalp --> goal pointer when DATA_IN
  3014. ** wlastp --> start pointer when DATA_OUT
  3015. ** wgoalp --> goal pointer when DATA_OUT
  3016. ** This script sets savep/lastp/goalp according to the
  3017. ** direction chosen by the target.
  3018. */
  3019. SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
  3020. 32,
  3021. /*
  3022. ** Direction is DATA IN.
  3023. ** Warning: we jump here, even when phase is DATA OUT.
  3024. */
  3025. SCR_COPY (4),
  3026. NADDR (header.lastp),
  3027. NADDR (header.savep),
  3028. /*
  3029. ** Jump to the SCRIPTS according to actual direction.
  3030. */
  3031. SCR_COPY (4),
  3032. NADDR (header.savep),
  3033. RADDR (temp),
  3034. SCR_RETURN,
  3035. 0,
  3036. /*
  3037. ** Direction is DATA OUT.
  3038. */
  3039. SCR_COPY (4),
  3040. NADDR (header.wlastp),
  3041. NADDR (header.lastp),
  3042. SCR_COPY (4),
  3043. NADDR (header.wgoalp),
  3044. NADDR (header.goalp),
  3045. SCR_JUMPR,
  3046. -64,
  3047. }/*-------------------------< BAD_IDENTIFY >---------------*/,{
  3048. /*
  3049. ** If message phase but not an IDENTIFY,
  3050. ** get some help from the C code.
  3051. ** Old SCSI device may behave so.
  3052. */
  3053. SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
  3054. 16,
  3055. SCR_INT,
  3056. SIR_RESEL_NO_IDENTIFY,
  3057. SCR_JUMP,
  3058. PADDRH (reset),
  3059. /*
  3060. ** Message is an IDENTIFY, but lun is unknown.
  3061. ** Read the message, since we got it directly
  3062. ** from the SCSI BUS data lines.
  3063. ** Signal problem to C code for logging the event.
  3064. ** Send an ABORT_TASK_SET to clear all pending tasks.
  3065. */
  3066. SCR_INT,
  3067. SIR_RESEL_BAD_LUN,
  3068. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  3069. NADDR (msgin),
  3070. SCR_JUMP,
  3071. PADDRH (abort),
  3072. }/*-------------------------< BAD_I_T_L >------------------*/,{
  3073. /*
  3074. ** We donnot have a task for that I_T_L.
  3075. ** Signal problem to C code for logging the event.
  3076. ** Send an ABORT_TASK_SET message.
  3077. */
  3078. SCR_INT,
  3079. SIR_RESEL_BAD_I_T_L,
  3080. SCR_JUMP,
  3081. PADDRH (abort),
  3082. }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
  3083. /*
  3084. ** We donnot have a task that matches the tag.
  3085. ** Signal problem to C code for logging the event.
  3086. ** Send an ABORT_TASK message.
  3087. */
  3088. SCR_INT,
  3089. SIR_RESEL_BAD_I_T_L_Q,
  3090. SCR_JUMP,
  3091. PADDRH (aborttag),
  3092. }/*-------------------------< BAD_TARGET >-----------------*/,{
  3093. /*
  3094. ** We donnot know the target that reselected us.
  3095. ** Grab the first message if any (IDENTIFY).
  3096. ** Signal problem to C code for logging the event.
  3097. ** TARGET_RESET message.
  3098. */
  3099. SCR_INT,
  3100. SIR_RESEL_BAD_TARGET,
  3101. SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
  3102. 8,
  3103. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  3104. NADDR (msgin),
  3105. SCR_JUMP,
  3106. PADDRH (reset),
  3107. }/*-------------------------< BAD_STATUS >-----------------*/,{
  3108. /*
  3109. ** If command resulted in either QUEUE FULL,
  3110. ** CHECK CONDITION or COMMAND TERMINATED,
  3111. ** call the C code.
  3112. */
  3113. SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
  3114. SIR_BAD_STATUS,
  3115. SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
  3116. SIR_BAD_STATUS,
  3117. SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
  3118. SIR_BAD_STATUS,
  3119. SCR_RETURN,
  3120. 0,
  3121. }/*-------------------------< START_RAM >-------------------*/,{
  3122. /*
  3123. ** Load the script into on-chip RAM,
  3124. ** and jump to start point.
  3125. */
  3126. SCR_COPY_F (4),
  3127. RADDR (scratcha),
  3128. PADDRH (start_ram0),
  3129. /*
  3130. ** Flush script prefetch if required
  3131. */
  3132. PREFETCH_FLUSH
  3133. SCR_COPY (sizeof (struct script)),
  3134. }/*-------------------------< START_RAM0 >--------------------*/,{
  3135. 0,
  3136. PADDR (start),
  3137. SCR_JUMP,
  3138. PADDR (start),
  3139. }/*-------------------------< STO_RESTART >-------------------*/,{
  3140. /*
  3141. **
  3142. ** Repair start queue (e.g. next time use the next slot)
  3143. ** and jump to start point.
  3144. */
  3145. SCR_COPY (4),
  3146. RADDR (temp),
  3147. PADDR (startpos),
  3148. SCR_JUMP,
  3149. PADDR (start),
  3150. }/*-------------------------< WAIT_DMA >-------------------*/,{
  3151. /*
  3152. ** For HP Zalon/53c720 systems, the Zalon interface
  3153. ** between CPU and 53c720 does prefetches, which causes
  3154. ** problems with self modifying scripts. The problem
  3155. ** is overcome by calling a dummy subroutine after each
  3156. ** modification, to force a refetch of the script on
  3157. ** return from the subroutine.
  3158. */
  3159. SCR_RETURN,
  3160. 0,
  3161. }/*-------------------------< SNOOPTEST >-------------------*/,{
  3162. /*
  3163. ** Read the variable.
  3164. */
  3165. SCR_COPY (4),
  3166. NADDR(ncr_cache),
  3167. RADDR (scratcha),
  3168. /*
  3169. ** Write the variable.
  3170. */
  3171. SCR_COPY (4),
  3172. RADDR (temp),
  3173. NADDR(ncr_cache),
  3174. /*
  3175. ** Read back the variable.
  3176. */
  3177. SCR_COPY (4),
  3178. NADDR(ncr_cache),
  3179. RADDR (temp),
  3180. }/*-------------------------< SNOOPEND >-------------------*/,{
  3181. /*
  3182. ** And stop.
  3183. */
  3184. SCR_INT,
  3185. 99,
  3186. }/*--------------------------------------------------------*/
  3187. };
  3188. /*==========================================================
  3189. **
  3190. **
  3191. ** Fill in #define dependent parts of the script
  3192. **
  3193. **
  3194. **==========================================================
  3195. */
  3196. void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
  3197. {
  3198. int i;
  3199. ncrcmd *p;
  3200. p = scrh->tryloop;
  3201. for (i=0; i<MAX_START; i++) {
  3202. *p++ =SCR_CALL;
  3203. *p++ =PADDR (idle);
  3204. }
  3205. BUG_ON((u_long)p != (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
  3206. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  3207. p = scrh->done_queue;
  3208. for (i = 0; i<MAX_DONE; i++) {
  3209. *p++ =SCR_COPY (sizeof(struct ccb *));
  3210. *p++ =NADDR (header.cp);
  3211. *p++ =NADDR (ccb_done[i]);
  3212. *p++ =SCR_CALL;
  3213. *p++ =PADDR (done_end);
  3214. }
  3215. BUG_ON((u_long)p != (u_long)&scrh->done_queue+sizeof(scrh->done_queue));
  3216. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  3217. p = scrh->hdata_in;
  3218. for (i=0; i<MAX_SCATTERH; i++) {
  3219. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
  3220. *p++ =PADDR (dispatch);
  3221. *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
  3222. *p++ =offsetof (struct dsb, data[i]);
  3223. }
  3224. BUG_ON((u_long)p != (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
  3225. p = scr->data_in;
  3226. for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
  3227. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
  3228. *p++ =PADDR (dispatch);
  3229. *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
  3230. *p++ =offsetof (struct dsb, data[i]);
  3231. }
  3232. BUG_ON((u_long)p != (u_long)&scr->data_in + sizeof (scr->data_in));
  3233. p = scrh->hdata_out;
  3234. for (i=0; i<MAX_SCATTERH; i++) {
  3235. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
  3236. *p++ =PADDR (dispatch);
  3237. *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
  3238. *p++ =offsetof (struct dsb, data[i]);
  3239. }
  3240. BUG_ON((u_long)p != (u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
  3241. p = scr->data_out;
  3242. for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
  3243. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
  3244. *p++ =PADDR (dispatch);
  3245. *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
  3246. *p++ =offsetof (struct dsb, data[i]);
  3247. }
  3248. BUG_ON((u_long) p != (u_long)&scr->data_out + sizeof (scr->data_out));
  3249. }
  3250. /*==========================================================
  3251. **
  3252. **
  3253. ** Copy and rebind a script.
  3254. **
  3255. **
  3256. **==========================================================
  3257. */
  3258. static void __init
  3259. ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
  3260. {
  3261. ncrcmd opcode, new, old, tmp1, tmp2;
  3262. ncrcmd *start, *end;
  3263. int relocs;
  3264. int opchanged = 0;
  3265. start = src;
  3266. end = src + len/4;
  3267. while (src < end) {
  3268. opcode = *src++;
  3269. *dst++ = cpu_to_scr(opcode);
  3270. /*
  3271. ** If we forget to change the length
  3272. ** in struct script, a field will be
  3273. ** padded with 0. This is an illegal
  3274. ** command.
  3275. */
  3276. if (opcode == 0) {
  3277. printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
  3278. ncr_name(np), (int) (src-start-1));
  3279. mdelay(1000);
  3280. }
  3281. if (DEBUG_FLAGS & DEBUG_SCRIPT)
  3282. printk (KERN_DEBUG "%p: <%x>\n",
  3283. (src-1), (unsigned)opcode);
  3284. /*
  3285. ** We don't have to decode ALL commands
  3286. */
  3287. switch (opcode >> 28) {
  3288. case 0xc:
  3289. /*
  3290. ** COPY has TWO arguments.
  3291. */
  3292. relocs = 2;
  3293. tmp1 = src[0];
  3294. #ifdef RELOC_KVAR
  3295. if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
  3296. tmp1 = 0;
  3297. #endif
  3298. tmp2 = src[1];
  3299. #ifdef RELOC_KVAR
  3300. if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
  3301. tmp2 = 0;
  3302. #endif
  3303. if ((tmp1 ^ tmp2) & 3) {
  3304. printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
  3305. ncr_name(np), (int) (src-start-1));
  3306. mdelay(1000);
  3307. }
  3308. /*
  3309. ** If PREFETCH feature not enabled, remove
  3310. ** the NO FLUSH bit if present.
  3311. */
  3312. if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
  3313. dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
  3314. ++opchanged;
  3315. }
  3316. break;
  3317. case 0x0:
  3318. /*
  3319. ** MOVE (absolute address)
  3320. */
  3321. relocs = 1;
  3322. break;
  3323. case 0x8:
  3324. /*
  3325. ** JUMP / CALL
  3326. ** don't relocate if relative :-)
  3327. */
  3328. if (opcode & 0x00800000)
  3329. relocs = 0;
  3330. else
  3331. relocs = 1;
  3332. break;
  3333. case 0x4:
  3334. case 0x5:
  3335. case 0x6:
  3336. case 0x7:
  3337. relocs = 1;
  3338. break;
  3339. default:
  3340. relocs = 0;
  3341. break;
  3342. }
  3343. if (relocs) {
  3344. while (relocs--) {
  3345. old = *src++;
  3346. switch (old & RELOC_MASK) {
  3347. case RELOC_REGISTER:
  3348. new = (old & ~RELOC_MASK) + np->paddr;
  3349. break;
  3350. case RELOC_LABEL:
  3351. new = (old & ~RELOC_MASK) + np->p_script;
  3352. break;
  3353. case RELOC_LABELH:
  3354. new = (old & ~RELOC_MASK) + np->p_scripth;
  3355. break;
  3356. case RELOC_SOFTC:
  3357. new = (old & ~RELOC_MASK) + np->p_ncb;
  3358. break;
  3359. #ifdef RELOC_KVAR
  3360. case RELOC_KVAR:
  3361. if (((old & ~RELOC_MASK) <
  3362. SCRIPT_KVAR_FIRST) ||
  3363. ((old & ~RELOC_MASK) >
  3364. SCRIPT_KVAR_LAST))
  3365. panic("ncr KVAR out of range");
  3366. new = vtophys(script_kvars[old &
  3367. ~RELOC_MASK]);
  3368. break;
  3369. #endif
  3370. case 0:
  3371. /* Don't relocate a 0 address. */
  3372. if (old == 0) {
  3373. new = old;
  3374. break;
  3375. }
  3376. /* fall through */
  3377. default:
  3378. panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
  3379. break;
  3380. }
  3381. *dst++ = cpu_to_scr(new);
  3382. }
  3383. } else
  3384. *dst++ = cpu_to_scr(*src++);
  3385. }
  3386. }
  3387. /*
  3388. ** Linux host data structure
  3389. */
  3390. struct host_data {
  3391. struct ncb *ncb;
  3392. };
  3393. #define PRINT_ADDR(cmd, arg...) dev_info(&cmd->device->sdev_gendev , ## arg)
  3394. static void ncr_print_msg(struct ccb *cp, char *label, u_char *msg)
  3395. {
  3396. PRINT_ADDR(cp->cmd, "%s: ", label);
  3397. spi_print_msg(msg);
  3398. printk("\n");
  3399. }
  3400. /*==========================================================
  3401. **
  3402. ** NCR chip clock divisor table.
  3403. ** Divisors are multiplied by 10,000,000 in order to make
  3404. ** calculations more simple.
  3405. **
  3406. **==========================================================
  3407. */
  3408. #define _5M 5000000
  3409. static u_long div_10M[] =
  3410. {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
  3411. /*===============================================================
  3412. **
  3413. ** Prepare io register values used by ncr_init() according
  3414. ** to selected and supported features.
  3415. **
  3416. ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
  3417. ** transfers. 32,64,128 are only supported by 875 and 895 chips.
  3418. ** We use log base 2 (burst length) as internal code, with
  3419. ** value 0 meaning "burst disabled".
  3420. **
  3421. **===============================================================
  3422. */
  3423. /*
  3424. * Burst length from burst code.
  3425. */
  3426. #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
  3427. /*
  3428. * Burst code from io register bits. Burst enable is ctest0 for c720
  3429. */
  3430. #define burst_code(dmode, ctest0) \
  3431. (ctest0) & 0x80 ? 0 : (((dmode) & 0xc0) >> 6) + 1
  3432. /*
  3433. * Set initial io register bits from burst code.
  3434. */
  3435. static inline void ncr_init_burst(struct ncb *np, u_char bc)
  3436. {
  3437. u_char *be = &np->rv_ctest0;
  3438. *be &= ~0x80;
  3439. np->rv_dmode &= ~(0x3 << 6);
  3440. np->rv_ctest5 &= ~0x4;
  3441. if (!bc) {
  3442. *be |= 0x80;
  3443. } else {
  3444. --bc;
  3445. np->rv_dmode |= ((bc & 0x3) << 6);
  3446. np->rv_ctest5 |= (bc & 0x4);
  3447. }
  3448. }
  3449. static void __init ncr_prepare_setting(struct ncb *np)
  3450. {
  3451. u_char burst_max;
  3452. u_long period;
  3453. int i;
  3454. /*
  3455. ** Save assumed BIOS setting
  3456. */
  3457. np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
  3458. np->sv_scntl3 = INB(nc_scntl3) & 0x07;
  3459. np->sv_dmode = INB(nc_dmode) & 0xce;
  3460. np->sv_dcntl = INB(nc_dcntl) & 0xa8;
  3461. np->sv_ctest0 = INB(nc_ctest0) & 0x84;
  3462. np->sv_ctest3 = INB(nc_ctest3) & 0x01;
  3463. np->sv_ctest4 = INB(nc_ctest4) & 0x80;
  3464. np->sv_ctest5 = INB(nc_ctest5) & 0x24;
  3465. np->sv_gpcntl = INB(nc_gpcntl);
  3466. np->sv_stest2 = INB(nc_stest2) & 0x20;
  3467. np->sv_stest4 = INB(nc_stest4);
  3468. /*
  3469. ** Wide ?
  3470. */
  3471. np->maxwide = (np->features & FE_WIDE)? 1 : 0;
  3472. /*
  3473. * Guess the frequency of the chip's clock.
  3474. */
  3475. if (np->features & FE_ULTRA)
  3476. np->clock_khz = 80000;
  3477. else
  3478. np->clock_khz = 40000;
  3479. /*
  3480. * Get the clock multiplier factor.
  3481. */
  3482. if (np->features & FE_QUAD)
  3483. np->multiplier = 4;
  3484. else if (np->features & FE_DBLR)
  3485. np->multiplier = 2;
  3486. else
  3487. np->multiplier = 1;
  3488. /*
  3489. * Measure SCSI clock frequency for chips
  3490. * it may vary from assumed one.
  3491. */
  3492. if (np->features & FE_VARCLK)
  3493. ncr_getclock(np, np->multiplier);
  3494. /*
  3495. * Divisor to be used for async (timer pre-scaler).
  3496. */
  3497. i = np->clock_divn - 1;
  3498. while (--i >= 0) {
  3499. if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
  3500. ++i;
  3501. break;
  3502. }
  3503. }
  3504. np->rv_scntl3 = i+1;
  3505. /*
  3506. * Minimum synchronous period factor supported by the chip.
  3507. * Btw, 'period' is in tenths of nanoseconds.
  3508. */
  3509. period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
  3510. if (period <= 250) np->minsync = 10;
  3511. else if (period <= 303) np->minsync = 11;
  3512. else if (period <= 500) np->minsync = 12;
  3513. else np->minsync = (period + 40 - 1) / 40;
  3514. /*
  3515. * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
  3516. */
  3517. if (np->minsync < 25 && !(np->features & FE_ULTRA))
  3518. np->minsync = 25;
  3519. /*
  3520. * Maximum synchronous period factor supported by the chip.
  3521. */
  3522. period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
  3523. np->maxsync = period > 2540 ? 254 : period / 10;
  3524. /*
  3525. ** Prepare initial value of other IO registers
  3526. */
  3527. #if defined SCSI_NCR_TRUST_BIOS_SETTING
  3528. np->rv_scntl0 = np->sv_scntl0;
  3529. np->rv_dmode = np->sv_dmode;
  3530. np->rv_dcntl = np->sv_dcntl;
  3531. np->rv_ctest0 = np->sv_ctest0;
  3532. np->rv_ctest3 = np->sv_ctest3;
  3533. np->rv_ctest4 = np->sv_ctest4;
  3534. np->rv_ctest5 = np->sv_ctest5;
  3535. burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
  3536. #else
  3537. /*
  3538. ** Select burst length (dwords)
  3539. */
  3540. burst_max = driver_setup.burst_max;
  3541. if (burst_max == 255)
  3542. burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
  3543. if (burst_max > 7)
  3544. burst_max = 7;
  3545. if (burst_max > np->maxburst)
  3546. burst_max = np->maxburst;
  3547. /*
  3548. ** Select all supported special features
  3549. */
  3550. if (np->features & FE_ERL)
  3551. np->rv_dmode |= ERL; /* Enable Read Line */
  3552. if (np->features & FE_BOF)
  3553. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  3554. if (np->features & FE_ERMP)
  3555. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  3556. if (np->features & FE_PFEN)
  3557. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  3558. if (np->features & FE_CLSE)
  3559. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  3560. if (np->features & FE_WRIE)
  3561. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  3562. if (np->features & FE_DFS)
  3563. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  3564. if (np->features & FE_MUX)
  3565. np->rv_ctest4 |= MUX; /* Host bus multiplex mode */
  3566. if (np->features & FE_EA)
  3567. np->rv_dcntl |= EA; /* Enable ACK */
  3568. if (np->features & FE_EHP)
  3569. np->rv_ctest0 |= EHP; /* Even host parity */
  3570. /*
  3571. ** Select some other
  3572. */
  3573. if (driver_setup.master_parity)
  3574. np->rv_ctest4 |= MPEE; /* Master parity checking */
  3575. if (driver_setup.scsi_parity)
  3576. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  3577. /*
  3578. ** Get SCSI addr of host adapter (set by bios?).
  3579. */
  3580. if (np->myaddr == 255) {
  3581. np->myaddr = INB(nc_scid) & 0x07;
  3582. if (!np->myaddr)
  3583. np->myaddr = SCSI_NCR_MYADDR;
  3584. }
  3585. #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
  3586. /*
  3587. * Prepare initial io register bits for burst length
  3588. */
  3589. ncr_init_burst(np, burst_max);
  3590. /*
  3591. ** Set SCSI BUS mode.
  3592. **
  3593. ** - ULTRA2 chips (895/895A/896) report the current
  3594. ** BUS mode through the STEST4 IO register.
  3595. ** - For previous generation chips (825/825A/875),
  3596. ** user has to tell us how to check against HVD,
  3597. ** since a 100% safe algorithm is not possible.
  3598. */
  3599. np->scsi_mode = SMODE_SE;
  3600. if (np->features & FE_DIFF) {
  3601. switch(driver_setup.diff_support) {
  3602. case 4: /* Trust previous settings if present, then GPIO3 */
  3603. if (np->sv_scntl3) {
  3604. if (np->sv_stest2 & 0x20)
  3605. np->scsi_mode = SMODE_HVD;
  3606. break;
  3607. }
  3608. case 3: /* SYMBIOS controllers report HVD through GPIO3 */
  3609. if (INB(nc_gpreg) & 0x08)
  3610. break;
  3611. case 2: /* Set HVD unconditionally */
  3612. np->scsi_mode = SMODE_HVD;
  3613. case 1: /* Trust previous settings for HVD */
  3614. if (np->sv_stest2 & 0x20)
  3615. np->scsi_mode = SMODE_HVD;
  3616. break;
  3617. default:/* Don't care about HVD */
  3618. break;
  3619. }
  3620. }
  3621. if (np->scsi_mode == SMODE_HVD)
  3622. np->rv_stest2 |= 0x20;
  3623. /*
  3624. ** Set LED support from SCRIPTS.
  3625. ** Ignore this feature for boards known to use a
  3626. ** specific GPIO wiring and for the 895A or 896
  3627. ** that drive the LED directly.
  3628. ** Also probe initial setting of GPIO0 as output.
  3629. */
  3630. if ((driver_setup.led_pin) &&
  3631. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  3632. np->features |= FE_LED0;
  3633. /*
  3634. ** Set irq mode.
  3635. */
  3636. switch(driver_setup.irqm & 3) {
  3637. case 2:
  3638. np->rv_dcntl |= IRQM;
  3639. break;
  3640. case 1:
  3641. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  3642. break;
  3643. default:
  3644. break;
  3645. }
  3646. /*
  3647. ** Configure targets according to driver setup.
  3648. ** Allow to override sync, wide and NOSCAN from
  3649. ** boot command line.
  3650. */
  3651. for (i = 0 ; i < MAX_TARGET ; i++) {
  3652. struct tcb *tp = &np->target[i];
  3653. tp->usrsync = driver_setup.default_sync;
  3654. tp->usrwide = driver_setup.max_wide;
  3655. tp->usrtags = MAX_TAGS;
  3656. tp->period = 0xffff;
  3657. if (!driver_setup.disconnection)
  3658. np->target[i].usrflag = UF_NODISC;
  3659. }
  3660. /*
  3661. ** Announce all that stuff to user.
  3662. */
  3663. printk(KERN_INFO "%s: ID %d, Fast-%d%s%s\n", ncr_name(np),
  3664. np->myaddr,
  3665. np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
  3666. (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
  3667. (np->rv_stest2 & 0x20) ? ", Differential" : "");
  3668. if (bootverbose > 1) {
  3669. printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  3670. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  3671. ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  3672. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  3673. printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  3674. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  3675. ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  3676. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  3677. }
  3678. if (bootverbose && np->paddr2)
  3679. printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
  3680. ncr_name(np), np->paddr2);
  3681. }
  3682. /*==========================================================
  3683. **
  3684. **
  3685. ** Done SCSI commands list management.
  3686. **
  3687. ** We donnot enter the scsi_done() callback immediately
  3688. ** after a command has been seen as completed but we
  3689. ** insert it into a list which is flushed outside any kind
  3690. ** of driver critical section.
  3691. ** This allows to do minimal stuff under interrupt and
  3692. ** inside critical sections and to also avoid locking up
  3693. ** on recursive calls to driver entry points under SMP.
  3694. ** In fact, the only kernel point which is entered by the
  3695. ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
  3696. ** that shall not reenter the driver under any circumstances,
  3697. ** AFAIK.
  3698. **
  3699. **==========================================================
  3700. */
  3701. static inline void ncr_queue_done_cmd(struct ncb *np, struct scsi_cmnd *cmd)
  3702. {
  3703. unmap_scsi_data(np, cmd);
  3704. cmd->host_scribble = (char *) np->done_list;
  3705. np->done_list = cmd;
  3706. }
  3707. static inline void ncr_flush_done_cmds(struct scsi_cmnd *lcmd)
  3708. {
  3709. struct scsi_cmnd *cmd;
  3710. while (lcmd) {
  3711. cmd = lcmd;
  3712. lcmd = (struct scsi_cmnd *) cmd->host_scribble;
  3713. cmd->scsi_done(cmd);
  3714. }
  3715. }
  3716. /*==========================================================
  3717. **
  3718. **
  3719. ** Prepare the next negotiation message if needed.
  3720. **
  3721. ** Fill in the part of message buffer that contains the
  3722. ** negotiation and the nego_status field of the CCB.
  3723. ** Returns the size of the message in bytes.
  3724. **
  3725. **
  3726. **==========================================================
  3727. */
  3728. static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
  3729. {
  3730. struct tcb *tp = &np->target[cp->target];
  3731. int msglen = 0;
  3732. int nego = 0;
  3733. struct scsi_target *starget = tp->starget;
  3734. /* negotiate wide transfers ? */
  3735. if (!tp->widedone) {
  3736. if (spi_support_wide(starget)) {
  3737. nego = NS_WIDE;
  3738. } else
  3739. tp->widedone=1;
  3740. }
  3741. /* negotiate synchronous transfers? */
  3742. if (!nego && !tp->period) {
  3743. if (spi_support_sync(starget)) {
  3744. nego = NS_SYNC;
  3745. } else {
  3746. tp->period =0xffff;
  3747. dev_info(&starget->dev, "target did not report SYNC.\n");
  3748. }
  3749. }
  3750. switch (nego) {
  3751. case NS_SYNC:
  3752. msglen += spi_populate_sync_msg(msgptr + msglen,
  3753. tp->maxoffs ? tp->minsync : 0, tp->maxoffs);
  3754. break;
  3755. case NS_WIDE:
  3756. msglen += spi_populate_width_msg(msgptr + msglen, tp->usrwide);
  3757. break;
  3758. }
  3759. cp->nego_status = nego;
  3760. if (nego) {
  3761. tp->nego_cp = cp;
  3762. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3763. ncr_print_msg(cp, nego == NS_WIDE ?
  3764. "wide msgout":"sync_msgout", msgptr);
  3765. }
  3766. }
  3767. return msglen;
  3768. }
  3769. /*==========================================================
  3770. **
  3771. **
  3772. ** Start execution of a SCSI command.
  3773. ** This is called from the generic SCSI driver.
  3774. **
  3775. **
  3776. **==========================================================
  3777. */
  3778. static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
  3779. {
  3780. struct scsi_device *sdev = cmd->device;
  3781. struct tcb *tp = &np->target[sdev->id];
  3782. struct lcb *lp = tp->lp[sdev->lun];
  3783. struct ccb *cp;
  3784. int segments;
  3785. u_char idmsg, *msgptr;
  3786. u32 msglen;
  3787. int direction;
  3788. u32 lastp, goalp;
  3789. /*---------------------------------------------
  3790. **
  3791. ** Some shortcuts ...
  3792. **
  3793. **---------------------------------------------
  3794. */
  3795. if ((sdev->id == np->myaddr ) ||
  3796. (sdev->id >= MAX_TARGET) ||
  3797. (sdev->lun >= MAX_LUN )) {
  3798. return(DID_BAD_TARGET);
  3799. }
  3800. /*---------------------------------------------
  3801. **
  3802. ** Complete the 1st TEST UNIT READY command
  3803. ** with error condition if the device is
  3804. ** flagged NOSCAN, in order to speed up
  3805. ** the boot.
  3806. **
  3807. **---------------------------------------------
  3808. */
  3809. if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) &&
  3810. (tp->usrflag & UF_NOSCAN)) {
  3811. tp->usrflag &= ~UF_NOSCAN;
  3812. return DID_BAD_TARGET;
  3813. }
  3814. if (DEBUG_FLAGS & DEBUG_TINY) {
  3815. PRINT_ADDR(cmd, "CMD=%x ", cmd->cmnd[0]);
  3816. }
  3817. /*---------------------------------------------------
  3818. **
  3819. ** Assign a ccb / bind cmd.
  3820. ** If resetting, shorten settle_time if necessary
  3821. ** in order to avoid spurious timeouts.
  3822. ** If resetting or no free ccb,
  3823. ** insert cmd into the waiting list.
  3824. **
  3825. **----------------------------------------------------
  3826. */
  3827. if (np->settle_time && cmd->timeout_per_command >= HZ) {
  3828. u_long tlimit = jiffies + cmd->timeout_per_command - HZ;
  3829. if (time_after(np->settle_time, tlimit))
  3830. np->settle_time = tlimit;
  3831. }
  3832. if (np->settle_time || !(cp=ncr_get_ccb (np, cmd))) {
  3833. insert_into_waiting_list(np, cmd);
  3834. return(DID_OK);
  3835. }
  3836. cp->cmd = cmd;
  3837. /*----------------------------------------------------
  3838. **
  3839. ** Build the identify / tag / sdtr message
  3840. **
  3841. **----------------------------------------------------
  3842. */
  3843. idmsg = IDENTIFY(0, sdev->lun);
  3844. if (cp ->tag != NO_TAG ||
  3845. (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
  3846. idmsg |= 0x40;
  3847. msgptr = cp->scsi_smsg;
  3848. msglen = 0;
  3849. msgptr[msglen++] = idmsg;
  3850. if (cp->tag != NO_TAG) {
  3851. char order = np->order;
  3852. /*
  3853. ** Force ordered tag if necessary to avoid timeouts
  3854. ** and to preserve interactivity.
  3855. */
  3856. if (lp && time_after(jiffies, lp->tags_stime)) {
  3857. if (lp->tags_smap) {
  3858. order = ORDERED_QUEUE_TAG;
  3859. if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
  3860. PRINT_ADDR(cmd,
  3861. "ordered tag forced.\n");
  3862. }
  3863. }
  3864. lp->tags_stime = jiffies + 3*HZ;
  3865. lp->tags_smap = lp->tags_umap;
  3866. }
  3867. if (order == 0) {
  3868. /*
  3869. ** Ordered write ops, unordered read ops.
  3870. */
  3871. switch (cmd->cmnd[0]) {
  3872. case 0x08: /* READ_SMALL (6) */
  3873. case 0x28: /* READ_BIG (10) */
  3874. case 0xa8: /* READ_HUGE (12) */
  3875. order = SIMPLE_QUEUE_TAG;
  3876. break;
  3877. default:
  3878. order = ORDERED_QUEUE_TAG;
  3879. }
  3880. }
  3881. msgptr[msglen++] = order;
  3882. /*
  3883. ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
  3884. ** since we may have to deal with devices that have
  3885. ** problems with #TAG 0 or too great #TAG numbers.
  3886. */
  3887. msgptr[msglen++] = (cp->tag << 1) + 1;
  3888. }
  3889. /*----------------------------------------------------
  3890. **
  3891. ** Build the data descriptors
  3892. **
  3893. **----------------------------------------------------
  3894. */
  3895. direction = cmd->sc_data_direction;
  3896. if (direction != DMA_NONE) {
  3897. segments = ncr_scatter(np, cp, cp->cmd);
  3898. if (segments < 0) {
  3899. ncr_free_ccb(np, cp);
  3900. return(DID_ERROR);
  3901. }
  3902. }
  3903. else {
  3904. cp->data_len = 0;
  3905. segments = 0;
  3906. }
  3907. /*---------------------------------------------------
  3908. **
  3909. ** negotiation required?
  3910. **
  3911. ** (nego_status is filled by ncr_prepare_nego())
  3912. **
  3913. **---------------------------------------------------
  3914. */
  3915. cp->nego_status = 0;
  3916. if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
  3917. msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
  3918. }
  3919. /*----------------------------------------------------
  3920. **
  3921. ** Determine xfer direction.
  3922. **
  3923. **----------------------------------------------------
  3924. */
  3925. if (!cp->data_len)
  3926. direction = DMA_NONE;
  3927. /*
  3928. ** If data direction is BIDIRECTIONAL, speculate FROM_DEVICE
  3929. ** but prepare alternate pointers for TO_DEVICE in case
  3930. ** of our speculation will be just wrong.
  3931. ** SCRIPTS will swap values if needed.
  3932. */
  3933. switch(direction) {
  3934. case DMA_BIDIRECTIONAL:
  3935. case DMA_TO_DEVICE:
  3936. goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
  3937. if (segments <= MAX_SCATTERL)
  3938. lastp = goalp - 8 - (segments * 16);
  3939. else {
  3940. lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
  3941. lastp -= (segments - MAX_SCATTERL) * 16;
  3942. }
  3943. if (direction != DMA_BIDIRECTIONAL)
  3944. break;
  3945. cp->phys.header.wgoalp = cpu_to_scr(goalp);
  3946. cp->phys.header.wlastp = cpu_to_scr(lastp);
  3947. /* fall through */
  3948. case DMA_FROM_DEVICE:
  3949. goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
  3950. if (segments <= MAX_SCATTERL)
  3951. lastp = goalp - 8 - (segments * 16);
  3952. else {
  3953. lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
  3954. lastp -= (segments - MAX_SCATTERL) * 16;
  3955. }
  3956. break;
  3957. default:
  3958. case DMA_NONE:
  3959. lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
  3960. break;
  3961. }
  3962. /*
  3963. ** Set all pointers values needed by SCRIPTS.
  3964. ** If direction is unknown, start at data_io.
  3965. */
  3966. cp->phys.header.lastp = cpu_to_scr(lastp);
  3967. cp->phys.header.goalp = cpu_to_scr(goalp);
  3968. if (direction == DMA_BIDIRECTIONAL)
  3969. cp->phys.header.savep =
  3970. cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
  3971. else
  3972. cp->phys.header.savep= cpu_to_scr(lastp);
  3973. /*
  3974. ** Save the initial data pointer in order to be able
  3975. ** to redo the command.
  3976. */
  3977. cp->startp = cp->phys.header.savep;
  3978. /*----------------------------------------------------
  3979. **
  3980. ** fill in ccb
  3981. **
  3982. **----------------------------------------------------
  3983. **
  3984. **
  3985. ** physical -> virtual backlink
  3986. ** Generic SCSI command
  3987. */
  3988. /*
  3989. ** Startqueue
  3990. */
  3991. cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  3992. cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
  3993. /*
  3994. ** select
  3995. */
  3996. cp->phys.select.sel_id = sdev_id(sdev);
  3997. cp->phys.select.sel_scntl3 = tp->wval;
  3998. cp->phys.select.sel_sxfer = tp->sval;
  3999. /*
  4000. ** message
  4001. */
  4002. cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
  4003. cp->phys.smsg.size = cpu_to_scr(msglen);
  4004. /*
  4005. ** command
  4006. */
  4007. memcpy(cp->cdb_buf, cmd->cmnd, min_t(int, cmd->cmd_len, sizeof(cp->cdb_buf)));
  4008. cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
  4009. cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
  4010. /*
  4011. ** status
  4012. */
  4013. cp->actualquirks = 0;
  4014. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  4015. cp->scsi_status = S_ILLEGAL;
  4016. cp->parity_status = 0;
  4017. cp->xerr_status = XE_OK;
  4018. #if 0
  4019. cp->sync_status = tp->sval;
  4020. cp->wide_status = tp->wval;
  4021. #endif
  4022. /*----------------------------------------------------
  4023. **
  4024. ** Critical region: start this job.
  4025. **
  4026. **----------------------------------------------------
  4027. */
  4028. /* activate this job. */
  4029. cp->magic = CCB_MAGIC;
  4030. /*
  4031. ** insert next CCBs into start queue.
  4032. ** 2 max at a time is enough to flush the CCB wait queue.
  4033. */
  4034. cp->auto_sense = 0;
  4035. if (lp)
  4036. ncr_start_next_ccb(np, lp, 2);
  4037. else
  4038. ncr_put_start_queue(np, cp);
  4039. /* Command is successfully queued. */
  4040. return DID_OK;
  4041. }
  4042. /*==========================================================
  4043. **
  4044. **
  4045. ** Insert a CCB into the start queue and wake up the
  4046. ** SCRIPTS processor.
  4047. **
  4048. **
  4049. **==========================================================
  4050. */
  4051. static void ncr_start_next_ccb(struct ncb *np, struct lcb *lp, int maxn)
  4052. {
  4053. struct list_head *qp;
  4054. struct ccb *cp;
  4055. if (lp->held_ccb)
  4056. return;
  4057. while (maxn-- && lp->queuedccbs < lp->queuedepth) {
  4058. qp = ncr_list_pop(&lp->wait_ccbq);
  4059. if (!qp)
  4060. break;
  4061. ++lp->queuedccbs;
  4062. cp = list_entry(qp, struct ccb, link_ccbq);
  4063. list_add_tail(qp, &lp->busy_ccbq);
  4064. lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
  4065. cpu_to_scr(CCB_PHYS (cp, restart));
  4066. ncr_put_start_queue(np, cp);
  4067. }
  4068. }
  4069. static void ncr_put_start_queue(struct ncb *np, struct ccb *cp)
  4070. {
  4071. u16 qidx;
  4072. /*
  4073. ** insert into start queue.
  4074. */
  4075. if (!np->squeueput) np->squeueput = 1;
  4076. qidx = np->squeueput + 2;
  4077. if (qidx >= MAX_START + MAX_START) qidx = 1;
  4078. np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  4079. MEMORY_BARRIER();
  4080. np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
  4081. np->squeueput = qidx;
  4082. ++np->queuedccbs;
  4083. cp->queued = 1;
  4084. if (DEBUG_FLAGS & DEBUG_QUEUE)
  4085. printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
  4086. /*
  4087. ** Script processor may be waiting for reselect.
  4088. ** Wake it up.
  4089. */
  4090. MEMORY_BARRIER();
  4091. OUTB (nc_istat, SIGP);
  4092. }
  4093. static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
  4094. {
  4095. u32 term;
  4096. int retv = 0;
  4097. np->settle_time = jiffies + settle_delay * HZ;
  4098. if (bootverbose > 1)
  4099. printk("%s: resetting, "
  4100. "command processing suspended for %d seconds\n",
  4101. ncr_name(np), settle_delay);
  4102. ncr_chip_reset(np, 100);
  4103. udelay(2000); /* The 895 needs time for the bus mode to settle */
  4104. if (enab_int)
  4105. OUTW (nc_sien, RST);
  4106. /*
  4107. ** Enable Tolerant, reset IRQD if present and
  4108. ** properly set IRQ mode, prior to resetting the bus.
  4109. */
  4110. OUTB (nc_stest3, TE);
  4111. OUTB (nc_scntl1, CRST);
  4112. udelay(200);
  4113. if (!driver_setup.bus_check)
  4114. goto out;
  4115. /*
  4116. ** Check for no terminators or SCSI bus shorts to ground.
  4117. ** Read SCSI data bus, data parity bits and control signals.
  4118. ** We are expecting RESET to be TRUE and other signals to be
  4119. ** FALSE.
  4120. */
  4121. term = INB(nc_sstat0);
  4122. term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
  4123. term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
  4124. ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
  4125. ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
  4126. INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
  4127. if (!(np->features & FE_WIDE))
  4128. term &= 0x3ffff;
  4129. if (term != (2<<7)) {
  4130. printk("%s: suspicious SCSI data while resetting the BUS.\n",
  4131. ncr_name(np));
  4132. printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
  4133. "0x%lx, expecting 0x%lx\n",
  4134. ncr_name(np),
  4135. (np->features & FE_WIDE) ? "dp1,d15-8," : "",
  4136. (u_long)term, (u_long)(2<<7));
  4137. if (driver_setup.bus_check == 1)
  4138. retv = 1;
  4139. }
  4140. out:
  4141. OUTB (nc_scntl1, 0);
  4142. return retv;
  4143. }
  4144. /*
  4145. * Start reset process.
  4146. * If reset in progress do nothing.
  4147. * The interrupt handler will reinitialize the chip.
  4148. * The timeout handler will wait for settle_time before
  4149. * clearing it and so resuming command processing.
  4150. */
  4151. static void ncr_start_reset(struct ncb *np)
  4152. {
  4153. if (!np->settle_time) {
  4154. ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
  4155. }
  4156. }
  4157. /*==========================================================
  4158. **
  4159. **
  4160. ** Reset the SCSI BUS.
  4161. ** This is called from the generic SCSI driver.
  4162. **
  4163. **
  4164. **==========================================================
  4165. */
  4166. static int ncr_reset_bus (struct ncb *np, struct scsi_cmnd *cmd, int sync_reset)
  4167. {
  4168. /* struct scsi_device *device = cmd->device; */
  4169. struct ccb *cp;
  4170. int found;
  4171. /*
  4172. * Return immediately if reset is in progress.
  4173. */
  4174. if (np->settle_time) {
  4175. return FAILED;
  4176. }
  4177. /*
  4178. * Start the reset process.
  4179. * The script processor is then assumed to be stopped.
  4180. * Commands will now be queued in the waiting list until a settle
  4181. * delay of 2 seconds will be completed.
  4182. */
  4183. ncr_start_reset(np);
  4184. /*
  4185. * First, look in the wakeup list
  4186. */
  4187. for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
  4188. /*
  4189. ** look for the ccb of this command.
  4190. */
  4191. if (cp->host_status == HS_IDLE) continue;
  4192. if (cp->cmd == cmd) {
  4193. found = 1;
  4194. break;
  4195. }
  4196. }
  4197. /*
  4198. * Then, look in the waiting list
  4199. */
  4200. if (!found && retrieve_from_waiting_list(0, np, cmd))
  4201. found = 1;
  4202. /*
  4203. * Wake-up all awaiting commands with DID_RESET.
  4204. */
  4205. reset_waiting_list(np);
  4206. /*
  4207. * Wake-up all pending commands with HS_RESET -> DID_RESET.
  4208. */
  4209. ncr_wakeup(np, HS_RESET);
  4210. /*
  4211. * If the involved command was not in a driver queue, and the
  4212. * scsi driver told us reset is synchronous, and the command is not
  4213. * currently in the waiting list, complete it with DID_RESET status,
  4214. * in order to keep it alive.
  4215. */
  4216. if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
  4217. cmd->result = ScsiResult(DID_RESET, 0);
  4218. ncr_queue_done_cmd(np, cmd);
  4219. }
  4220. return SUCCESS;
  4221. }
  4222. #if 0 /* unused and broken.. */
  4223. /*==========================================================
  4224. **
  4225. **
  4226. ** Abort an SCSI command.
  4227. ** This is called from the generic SCSI driver.
  4228. **
  4229. **
  4230. **==========================================================
  4231. */
  4232. static int ncr_abort_command (struct ncb *np, struct scsi_cmnd *cmd)
  4233. {
  4234. /* struct scsi_device *device = cmd->device; */
  4235. struct ccb *cp;
  4236. int found;
  4237. int retv;
  4238. /*
  4239. * First, look for the scsi command in the waiting list
  4240. */
  4241. if (remove_from_waiting_list(np, cmd)) {
  4242. cmd->result = ScsiResult(DID_ABORT, 0);
  4243. ncr_queue_done_cmd(np, cmd);
  4244. return SCSI_ABORT_SUCCESS;
  4245. }
  4246. /*
  4247. * Then, look in the wakeup list
  4248. */
  4249. for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
  4250. /*
  4251. ** look for the ccb of this command.
  4252. */
  4253. if (cp->host_status == HS_IDLE) continue;
  4254. if (cp->cmd == cmd) {
  4255. found = 1;
  4256. break;
  4257. }
  4258. }
  4259. if (!found) {
  4260. return SCSI_ABORT_NOT_RUNNING;
  4261. }
  4262. if (np->settle_time) {
  4263. return SCSI_ABORT_SNOOZE;
  4264. }
  4265. /*
  4266. ** If the CCB is active, patch schedule jumps for the
  4267. ** script to abort the command.
  4268. */
  4269. switch(cp->host_status) {
  4270. case HS_BUSY:
  4271. case HS_NEGOTIATE:
  4272. printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
  4273. cp->start.schedule.l_paddr =
  4274. cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
  4275. retv = SCSI_ABORT_PENDING;
  4276. break;
  4277. case HS_DISCONNECT:
  4278. cp->restart.schedule.l_paddr =
  4279. cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
  4280. retv = SCSI_ABORT_PENDING;
  4281. break;
  4282. default:
  4283. retv = SCSI_ABORT_NOT_RUNNING;
  4284. break;
  4285. }
  4286. /*
  4287. ** If there are no requests, the script
  4288. ** processor will sleep on SEL_WAIT_RESEL.
  4289. ** Let's wake it up, since it may have to work.
  4290. */
  4291. OUTB (nc_istat, SIGP);
  4292. return retv;
  4293. }
  4294. #endif
  4295. static void ncr_detach(struct ncb *np)
  4296. {
  4297. struct ccb *cp;
  4298. struct tcb *tp;
  4299. struct lcb *lp;
  4300. int target, lun;
  4301. int i;
  4302. char inst_name[16];
  4303. /* Local copy so we don't access np after freeing it! */
  4304. strlcpy(inst_name, ncr_name(np), sizeof(inst_name));
  4305. printk("%s: releasing host resources\n", ncr_name(np));
  4306. /*
  4307. ** Stop the ncr_timeout process
  4308. ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
  4309. */
  4310. #ifdef DEBUG_NCR53C8XX
  4311. printk("%s: stopping the timer\n", ncr_name(np));
  4312. #endif
  4313. np->release_stage = 1;
  4314. for (i = 50 ; i && np->release_stage != 2 ; i--)
  4315. mdelay(100);
  4316. if (np->release_stage != 2)
  4317. printk("%s: the timer seems to be already stopped\n", ncr_name(np));
  4318. else np->release_stage = 2;
  4319. /*
  4320. ** Disable chip interrupts
  4321. */
  4322. #ifdef DEBUG_NCR53C8XX
  4323. printk("%s: disabling chip interrupts\n", ncr_name(np));
  4324. #endif
  4325. OUTW (nc_sien , 0);
  4326. OUTB (nc_dien , 0);
  4327. /*
  4328. ** Reset NCR chip
  4329. ** Restore bios setting for automatic clock detection.
  4330. */
  4331. printk("%s: resetting chip\n", ncr_name(np));
  4332. ncr_chip_reset(np, 100);
  4333. OUTB(nc_dmode, np->sv_dmode);
  4334. OUTB(nc_dcntl, np->sv_dcntl);
  4335. OUTB(nc_ctest0, np->sv_ctest0);
  4336. OUTB(nc_ctest3, np->sv_ctest3);
  4337. OUTB(nc_ctest4, np->sv_ctest4);
  4338. OUTB(nc_ctest5, np->sv_ctest5);
  4339. OUTB(nc_gpcntl, np->sv_gpcntl);
  4340. OUTB(nc_stest2, np->sv_stest2);
  4341. ncr_selectclock(np, np->sv_scntl3);
  4342. /*
  4343. ** Free allocated ccb(s)
  4344. */
  4345. while ((cp=np->ccb->link_ccb) != NULL) {
  4346. np->ccb->link_ccb = cp->link_ccb;
  4347. if (cp->host_status) {
  4348. printk("%s: shall free an active ccb (host_status=%d)\n",
  4349. ncr_name(np), cp->host_status);
  4350. }
  4351. #ifdef DEBUG_NCR53C8XX
  4352. printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
  4353. #endif
  4354. m_free_dma(cp, sizeof(*cp), "CCB");
  4355. }
  4356. /* Free allocated tp(s) */
  4357. for (target = 0; target < MAX_TARGET ; target++) {
  4358. tp=&np->target[target];
  4359. for (lun = 0 ; lun < MAX_LUN ; lun++) {
  4360. lp = tp->lp[lun];
  4361. if (lp) {
  4362. #ifdef DEBUG_NCR53C8XX
  4363. printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
  4364. #endif
  4365. if (lp->jump_ccb != &lp->jump_ccb_0)
  4366. m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
  4367. m_free_dma(lp, sizeof(*lp), "LCB");
  4368. }
  4369. }
  4370. }
  4371. if (np->scripth0)
  4372. m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
  4373. if (np->script0)
  4374. m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
  4375. if (np->ccb)
  4376. m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
  4377. m_free_dma(np, sizeof(struct ncb), "NCB");
  4378. printk("%s: host resources successfully released\n", inst_name);
  4379. }
  4380. /*==========================================================
  4381. **
  4382. **
  4383. ** Complete execution of a SCSI command.
  4384. ** Signal completion to the generic SCSI driver.
  4385. **
  4386. **
  4387. **==========================================================
  4388. */
  4389. void ncr_complete (struct ncb *np, struct ccb *cp)
  4390. {
  4391. struct scsi_cmnd *cmd;
  4392. struct tcb *tp;
  4393. struct lcb *lp;
  4394. /*
  4395. ** Sanity check
  4396. */
  4397. if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
  4398. return;
  4399. /*
  4400. ** Print minimal debug information.
  4401. */
  4402. if (DEBUG_FLAGS & DEBUG_TINY)
  4403. printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
  4404. cp->host_status,cp->scsi_status);
  4405. /*
  4406. ** Get command, target and lun pointers.
  4407. */
  4408. cmd = cp->cmd;
  4409. cp->cmd = NULL;
  4410. tp = &np->target[cmd->device->id];
  4411. lp = tp->lp[cmd->device->lun];
  4412. /*
  4413. ** We donnot queue more than 1 ccb per target
  4414. ** with negotiation at any time. If this ccb was
  4415. ** used for negotiation, clear this info in the tcb.
  4416. */
  4417. if (cp == tp->nego_cp)
  4418. tp->nego_cp = NULL;
  4419. /*
  4420. ** If auto-sense performed, change scsi status.
  4421. */
  4422. if (cp->auto_sense) {
  4423. cp->scsi_status = cp->auto_sense;
  4424. }
  4425. /*
  4426. ** If we were recovering from queue full or performing
  4427. ** auto-sense, requeue skipped CCBs to the wait queue.
  4428. */
  4429. if (lp && lp->held_ccb) {
  4430. if (cp == lp->held_ccb) {
  4431. list_splice_init(&lp->skip_ccbq, &lp->wait_ccbq);
  4432. lp->held_ccb = NULL;
  4433. }
  4434. }
  4435. /*
  4436. ** Check for parity errors.
  4437. */
  4438. if (cp->parity_status > 1) {
  4439. PRINT_ADDR(cmd, "%d parity error(s).\n",cp->parity_status);
  4440. }
  4441. /*
  4442. ** Check for extended errors.
  4443. */
  4444. if (cp->xerr_status != XE_OK) {
  4445. switch (cp->xerr_status) {
  4446. case XE_EXTRA_DATA:
  4447. PRINT_ADDR(cmd, "extraneous data discarded.\n");
  4448. break;
  4449. case XE_BAD_PHASE:
  4450. PRINT_ADDR(cmd, "invalid scsi phase (4/5).\n");
  4451. break;
  4452. default:
  4453. PRINT_ADDR(cmd, "extended error %d.\n",
  4454. cp->xerr_status);
  4455. break;
  4456. }
  4457. if (cp->host_status==HS_COMPLETE)
  4458. cp->host_status = HS_FAIL;
  4459. }
  4460. /*
  4461. ** Print out any error for debugging purpose.
  4462. */
  4463. if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
  4464. if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
  4465. PRINT_ADDR(cmd, "ERROR: cmd=%x host_status=%x "
  4466. "scsi_status=%x\n", cmd->cmnd[0],
  4467. cp->host_status, cp->scsi_status);
  4468. }
  4469. }
  4470. /*
  4471. ** Check the status.
  4472. */
  4473. if ( (cp->host_status == HS_COMPLETE)
  4474. && (cp->scsi_status == S_GOOD ||
  4475. cp->scsi_status == S_COND_MET)) {
  4476. /*
  4477. * All went well (GOOD status).
  4478. * CONDITION MET status is returned on
  4479. * `Pre-Fetch' or `Search data' success.
  4480. */
  4481. cmd->result = ScsiResult(DID_OK, cp->scsi_status);
  4482. /*
  4483. ** @RESID@
  4484. ** Could dig out the correct value for resid,
  4485. ** but it would be quite complicated.
  4486. */
  4487. /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
  4488. /*
  4489. ** Allocate the lcb if not yet.
  4490. */
  4491. if (!lp)
  4492. ncr_alloc_lcb (np, cmd->device->id, cmd->device->lun);
  4493. tp->bytes += cp->data_len;
  4494. tp->transfers ++;
  4495. /*
  4496. ** If tags was reduced due to queue full,
  4497. ** increase tags if 1000 good status received.
  4498. */
  4499. if (lp && lp->usetags && lp->numtags < lp->maxtags) {
  4500. ++lp->num_good;
  4501. if (lp->num_good >= 1000) {
  4502. lp->num_good = 0;
  4503. ++lp->numtags;
  4504. ncr_setup_tags (np, cmd->device);
  4505. }
  4506. }
  4507. } else if ((cp->host_status == HS_COMPLETE)
  4508. && (cp->scsi_status == S_CHECK_COND)) {
  4509. /*
  4510. ** Check condition code
  4511. */
  4512. cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
  4513. /*
  4514. ** Copy back sense data to caller's buffer.
  4515. */
  4516. memcpy(cmd->sense_buffer, cp->sense_buf,
  4517. min(sizeof(cmd->sense_buffer), sizeof(cp->sense_buf)));
  4518. if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
  4519. u_char * p = (u_char*) & cmd->sense_buffer;
  4520. int i;
  4521. PRINT_ADDR(cmd, "sense data:");
  4522. for (i=0; i<14; i++) printk (" %x", *p++);
  4523. printk (".\n");
  4524. }
  4525. } else if ((cp->host_status == HS_COMPLETE)
  4526. && (cp->scsi_status == S_CONFLICT)) {
  4527. /*
  4528. ** Reservation Conflict condition code
  4529. */
  4530. cmd->result = ScsiResult(DID_OK, S_CONFLICT);
  4531. } else if ((cp->host_status == HS_COMPLETE)
  4532. && (cp->scsi_status == S_BUSY ||
  4533. cp->scsi_status == S_QUEUE_FULL)) {
  4534. /*
  4535. ** Target is busy.
  4536. */
  4537. cmd->result = ScsiResult(DID_OK, cp->scsi_status);
  4538. } else if ((cp->host_status == HS_SEL_TIMEOUT)
  4539. || (cp->host_status == HS_TIMEOUT)) {
  4540. /*
  4541. ** No response
  4542. */
  4543. cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
  4544. } else if (cp->host_status == HS_RESET) {
  4545. /*
  4546. ** SCSI bus reset
  4547. */
  4548. cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
  4549. } else if (cp->host_status == HS_ABORTED) {
  4550. /*
  4551. ** Transfer aborted
  4552. */
  4553. cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
  4554. } else {
  4555. /*
  4556. ** Other protocol messes
  4557. */
  4558. PRINT_ADDR(cmd, "COMMAND FAILED (%x %x) @%p.\n",
  4559. cp->host_status, cp->scsi_status, cp);
  4560. cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
  4561. }
  4562. /*
  4563. ** trace output
  4564. */
  4565. if (tp->usrflag & UF_TRACE) {
  4566. u_char * p;
  4567. int i;
  4568. PRINT_ADDR(cmd, " CMD:");
  4569. p = (u_char*) &cmd->cmnd[0];
  4570. for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
  4571. if (cp->host_status==HS_COMPLETE) {
  4572. switch (cp->scsi_status) {
  4573. case S_GOOD:
  4574. printk (" GOOD");
  4575. break;
  4576. case S_CHECK_COND:
  4577. printk (" SENSE:");
  4578. p = (u_char*) &cmd->sense_buffer;
  4579. for (i=0; i<14; i++)
  4580. printk (" %x", *p++);
  4581. break;
  4582. default:
  4583. printk (" STAT: %x\n", cp->scsi_status);
  4584. break;
  4585. }
  4586. } else printk (" HOSTERROR: %x", cp->host_status);
  4587. printk ("\n");
  4588. }
  4589. /*
  4590. ** Free this ccb
  4591. */
  4592. ncr_free_ccb (np, cp);
  4593. /*
  4594. ** requeue awaiting scsi commands for this lun.
  4595. */
  4596. if (lp && lp->queuedccbs < lp->queuedepth &&
  4597. !list_empty(&lp->wait_ccbq))
  4598. ncr_start_next_ccb(np, lp, 2);
  4599. /*
  4600. ** requeue awaiting scsi commands for this controller.
  4601. */
  4602. if (np->waiting_list)
  4603. requeue_waiting_list(np);
  4604. /*
  4605. ** signal completion to generic driver.
  4606. */
  4607. ncr_queue_done_cmd(np, cmd);
  4608. }
  4609. /*==========================================================
  4610. **
  4611. **
  4612. ** Signal all (or one) control block done.
  4613. **
  4614. **
  4615. **==========================================================
  4616. */
  4617. /*
  4618. ** This CCB has been skipped by the NCR.
  4619. ** Queue it in the correponding unit queue.
  4620. */
  4621. static void ncr_ccb_skipped(struct ncb *np, struct ccb *cp)
  4622. {
  4623. struct tcb *tp = &np->target[cp->target];
  4624. struct lcb *lp = tp->lp[cp->lun];
  4625. if (lp && cp != np->ccb) {
  4626. cp->host_status &= ~HS_SKIPMASK;
  4627. cp->start.schedule.l_paddr =
  4628. cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  4629. list_del(&cp->link_ccbq);
  4630. list_add_tail(&cp->link_ccbq, &lp->skip_ccbq);
  4631. if (cp->queued) {
  4632. --lp->queuedccbs;
  4633. }
  4634. }
  4635. if (cp->queued) {
  4636. --np->queuedccbs;
  4637. cp->queued = 0;
  4638. }
  4639. }
  4640. /*
  4641. ** The NCR has completed CCBs.
  4642. ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
  4643. */
  4644. void ncr_wakeup_done (struct ncb *np)
  4645. {
  4646. struct ccb *cp;
  4647. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  4648. int i, j;
  4649. i = np->ccb_done_ic;
  4650. while (1) {
  4651. j = i+1;
  4652. if (j >= MAX_DONE)
  4653. j = 0;
  4654. cp = np->ccb_done[j];
  4655. if (!CCB_DONE_VALID(cp))
  4656. break;
  4657. np->ccb_done[j] = (struct ccb *)CCB_DONE_EMPTY;
  4658. np->scripth->done_queue[5*j + 4] =
  4659. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
  4660. MEMORY_BARRIER();
  4661. np->scripth->done_queue[5*i + 4] =
  4662. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
  4663. if (cp->host_status & HS_DONEMASK)
  4664. ncr_complete (np, cp);
  4665. else if (cp->host_status & HS_SKIPMASK)
  4666. ncr_ccb_skipped (np, cp);
  4667. i = j;
  4668. }
  4669. np->ccb_done_ic = i;
  4670. #else
  4671. cp = np->ccb;
  4672. while (cp) {
  4673. if (cp->host_status & HS_DONEMASK)
  4674. ncr_complete (np, cp);
  4675. else if (cp->host_status & HS_SKIPMASK)
  4676. ncr_ccb_skipped (np, cp);
  4677. cp = cp->link_ccb;
  4678. }
  4679. #endif
  4680. }
  4681. /*
  4682. ** Complete all active CCBs.
  4683. */
  4684. void ncr_wakeup (struct ncb *np, u_long code)
  4685. {
  4686. struct ccb *cp = np->ccb;
  4687. while (cp) {
  4688. if (cp->host_status != HS_IDLE) {
  4689. cp->host_status = code;
  4690. ncr_complete (np, cp);
  4691. }
  4692. cp = cp->link_ccb;
  4693. }
  4694. }
  4695. /*
  4696. ** Reset ncr chip.
  4697. */
  4698. /* Some initialisation must be done immediately following reset, for 53c720,
  4699. * at least. EA (dcntl bit 5) isn't set here as it is set once only in
  4700. * the _detect function.
  4701. */
  4702. static void ncr_chip_reset(struct ncb *np, int delay)
  4703. {
  4704. OUTB (nc_istat, SRST);
  4705. udelay(delay);
  4706. OUTB (nc_istat, 0 );
  4707. if (np->features & FE_EHP)
  4708. OUTB (nc_ctest0, EHP);
  4709. if (np->features & FE_MUX)
  4710. OUTB (nc_ctest4, MUX);
  4711. }
  4712. /*==========================================================
  4713. **
  4714. **
  4715. ** Start NCR chip.
  4716. **
  4717. **
  4718. **==========================================================
  4719. */
  4720. void ncr_init (struct ncb *np, int reset, char * msg, u_long code)
  4721. {
  4722. int i;
  4723. /*
  4724. ** Reset chip if asked, otherwise just clear fifos.
  4725. */
  4726. if (reset) {
  4727. OUTB (nc_istat, SRST);
  4728. udelay(100);
  4729. }
  4730. else {
  4731. OUTB (nc_stest3, TE|CSF);
  4732. OUTONB (nc_ctest3, CLF);
  4733. }
  4734. /*
  4735. ** Message.
  4736. */
  4737. if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
  4738. /*
  4739. ** Clear Start Queue
  4740. */
  4741. np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
  4742. for (i = 1; i < MAX_START + MAX_START; i += 2)
  4743. np->scripth0->tryloop[i] =
  4744. cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  4745. /*
  4746. ** Start at first entry.
  4747. */
  4748. np->squeueput = 0;
  4749. np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
  4750. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  4751. /*
  4752. ** Clear Done Queue
  4753. */
  4754. for (i = 0; i < MAX_DONE; i++) {
  4755. np->ccb_done[i] = (struct ccb *)CCB_DONE_EMPTY;
  4756. np->scripth0->done_queue[5*i + 4] =
  4757. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
  4758. }
  4759. #endif
  4760. /*
  4761. ** Start at first entry.
  4762. */
  4763. np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
  4764. np->ccb_done_ic = MAX_DONE-1;
  4765. np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
  4766. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
  4767. /*
  4768. ** Wakeup all pending jobs.
  4769. */
  4770. ncr_wakeup (np, code);
  4771. /*
  4772. ** Init chip.
  4773. */
  4774. /*
  4775. ** Remove reset; big delay because the 895 needs time for the
  4776. ** bus mode to settle
  4777. */
  4778. ncr_chip_reset(np, 2000);
  4779. OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
  4780. /* full arb., ena parity, par->ATN */
  4781. OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
  4782. ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
  4783. OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
  4784. OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
  4785. OUTB (nc_istat , SIGP ); /* Signal Process */
  4786. OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
  4787. OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
  4788. OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
  4789. OUTB (nc_ctest0, np->rv_ctest0); /* 720: CDIS and EHP */
  4790. OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
  4791. OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
  4792. OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
  4793. OUTB (nc_stest3, TE); /* TolerANT enable */
  4794. OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
  4795. /*
  4796. ** Disable disconnects.
  4797. */
  4798. np->disc = 0;
  4799. /*
  4800. ** Enable GPIO0 pin for writing if LED support.
  4801. */
  4802. if (np->features & FE_LED0) {
  4803. OUTOFFB (nc_gpcntl, 0x01);
  4804. }
  4805. /*
  4806. ** enable ints
  4807. */
  4808. OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
  4809. OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
  4810. /*
  4811. ** Fill in target structure.
  4812. ** Reinitialize usrsync.
  4813. ** Reinitialize usrwide.
  4814. ** Prepare sync negotiation according to actual SCSI bus mode.
  4815. */
  4816. for (i=0;i<MAX_TARGET;i++) {
  4817. struct tcb *tp = &np->target[i];
  4818. tp->sval = 0;
  4819. tp->wval = np->rv_scntl3;
  4820. if (tp->usrsync != 255) {
  4821. if (tp->usrsync <= np->maxsync) {
  4822. if (tp->usrsync < np->minsync) {
  4823. tp->usrsync = np->minsync;
  4824. }
  4825. }
  4826. else
  4827. tp->usrsync = 255;
  4828. }
  4829. if (tp->usrwide > np->maxwide)
  4830. tp->usrwide = np->maxwide;
  4831. }
  4832. /*
  4833. ** Start script processor.
  4834. */
  4835. if (np->paddr2) {
  4836. if (bootverbose)
  4837. printk ("%s: Downloading SCSI SCRIPTS.\n",
  4838. ncr_name(np));
  4839. OUTL (nc_scratcha, vtobus(np->script0));
  4840. OUTL_DSP (NCB_SCRIPTH_PHYS (np, start_ram));
  4841. }
  4842. else
  4843. OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
  4844. }
  4845. /*==========================================================
  4846. **
  4847. ** Prepare the negotiation values for wide and
  4848. ** synchronous transfers.
  4849. **
  4850. **==========================================================
  4851. */
  4852. static void ncr_negotiate (struct ncb* np, struct tcb* tp)
  4853. {
  4854. /*
  4855. ** minsync unit is 4ns !
  4856. */
  4857. u_long minsync = tp->usrsync;
  4858. /*
  4859. ** SCSI bus mode limit
  4860. */
  4861. if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
  4862. if (minsync < 12) minsync = 12;
  4863. }
  4864. /*
  4865. ** our limit ..
  4866. */
  4867. if (minsync < np->minsync)
  4868. minsync = np->minsync;
  4869. /*
  4870. ** divider limit
  4871. */
  4872. if (minsync > np->maxsync)
  4873. minsync = 255;
  4874. if (tp->maxoffs > np->maxoffs)
  4875. tp->maxoffs = np->maxoffs;
  4876. tp->minsync = minsync;
  4877. tp->maxoffs = (minsync<255 ? tp->maxoffs : 0);
  4878. /*
  4879. ** period=0: has to negotiate sync transfer
  4880. */
  4881. tp->period=0;
  4882. /*
  4883. ** widedone=0: has to negotiate wide transfer
  4884. */
  4885. tp->widedone=0;
  4886. }
  4887. /*==========================================================
  4888. **
  4889. ** Get clock factor and sync divisor for a given
  4890. ** synchronous factor period.
  4891. ** Returns the clock factor (in sxfer) and scntl3
  4892. ** synchronous divisor field.
  4893. **
  4894. **==========================================================
  4895. */
  4896. static void ncr_getsync(struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p)
  4897. {
  4898. u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
  4899. int div = np->clock_divn; /* Number of divisors supported */
  4900. u_long fak; /* Sync factor in sxfer */
  4901. u_long per; /* Period in tenths of ns */
  4902. u_long kpc; /* (per * clk) */
  4903. /*
  4904. ** Compute the synchronous period in tenths of nano-seconds
  4905. */
  4906. if (sfac <= 10) per = 250;
  4907. else if (sfac == 11) per = 303;
  4908. else if (sfac == 12) per = 500;
  4909. else per = 40 * sfac;
  4910. /*
  4911. ** Look for the greatest clock divisor that allows an
  4912. ** input speed faster than the period.
  4913. */
  4914. kpc = per * clk;
  4915. while (--div >= 0)
  4916. if (kpc >= (div_10M[div] << 2)) break;
  4917. /*
  4918. ** Calculate the lowest clock factor that allows an output
  4919. ** speed not faster than the period.
  4920. */
  4921. fak = (kpc - 1) / div_10M[div] + 1;
  4922. #if 0 /* This optimization does not seem very useful */
  4923. per = (fak * div_10M[div]) / clk;
  4924. /*
  4925. ** Why not to try the immediate lower divisor and to choose
  4926. ** the one that allows the fastest output speed ?
  4927. ** We don't want input speed too much greater than output speed.
  4928. */
  4929. if (div >= 1 && fak < 8) {
  4930. u_long fak2, per2;
  4931. fak2 = (kpc - 1) / div_10M[div-1] + 1;
  4932. per2 = (fak2 * div_10M[div-1]) / clk;
  4933. if (per2 < per && fak2 <= 8) {
  4934. fak = fak2;
  4935. per = per2;
  4936. --div;
  4937. }
  4938. }
  4939. #endif
  4940. if (fak < 4) fak = 4; /* Should never happen, too bad ... */
  4941. /*
  4942. ** Compute and return sync parameters for the ncr
  4943. */
  4944. *fakp = fak - 4;
  4945. *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
  4946. }
  4947. /*==========================================================
  4948. **
  4949. ** Set actual values, sync status and patch all ccbs of
  4950. ** a target according to new sync/wide agreement.
  4951. **
  4952. **==========================================================
  4953. */
  4954. static void ncr_set_sync_wide_status (struct ncb *np, u_char target)
  4955. {
  4956. struct ccb *cp;
  4957. struct tcb *tp = &np->target[target];
  4958. /*
  4959. ** set actual value and sync_status
  4960. */
  4961. OUTB (nc_sxfer, tp->sval);
  4962. np->sync_st = tp->sval;
  4963. OUTB (nc_scntl3, tp->wval);
  4964. np->wide_st = tp->wval;
  4965. /*
  4966. ** patch ALL ccbs of this target.
  4967. */
  4968. for (cp = np->ccb; cp; cp = cp->link_ccb) {
  4969. if (!cp->cmd) continue;
  4970. if (scmd_id(cp->cmd) != target) continue;
  4971. #if 0
  4972. cp->sync_status = tp->sval;
  4973. cp->wide_status = tp->wval;
  4974. #endif
  4975. cp->phys.select.sel_scntl3 = tp->wval;
  4976. cp->phys.select.sel_sxfer = tp->sval;
  4977. }
  4978. }
  4979. /*==========================================================
  4980. **
  4981. ** Switch sync mode for current job and it's target
  4982. **
  4983. **==========================================================
  4984. */
  4985. static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer)
  4986. {
  4987. struct scsi_cmnd *cmd = cp->cmd;
  4988. struct tcb *tp;
  4989. u_char target = INB (nc_sdid) & 0x0f;
  4990. u_char idiv;
  4991. BUG_ON(target != (scmd_id(cmd) & 0xf));
  4992. tp = &np->target[target];
  4993. if (!scntl3 || !(sxfer & 0x1f))
  4994. scntl3 = np->rv_scntl3;
  4995. scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
  4996. /*
  4997. ** Deduce the value of controller sync period from scntl3.
  4998. ** period is in tenths of nano-seconds.
  4999. */
  5000. idiv = ((scntl3 >> 4) & 0x7);
  5001. if ((sxfer & 0x1f) && idiv)
  5002. tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
  5003. else
  5004. tp->period = 0xffff;
  5005. /* Stop there if sync parameters are unchanged */
  5006. if (tp->sval == sxfer && tp->wval == scntl3)
  5007. return;
  5008. tp->sval = sxfer;
  5009. tp->wval = scntl3;
  5010. if (sxfer & 0x01f) {
  5011. /* Disable extended Sreq/Sack filtering */
  5012. if (tp->period <= 2000)
  5013. OUTOFFB(nc_stest2, EXT);
  5014. }
  5015. spi_display_xfer_agreement(tp->starget);
  5016. /*
  5017. ** set actual value and sync_status
  5018. ** patch ALL ccbs of this target.
  5019. */
  5020. ncr_set_sync_wide_status(np, target);
  5021. }
  5022. /*==========================================================
  5023. **
  5024. ** Switch wide mode for current job and it's target
  5025. ** SCSI specs say: a SCSI device that accepts a WDTR
  5026. ** message shall reset the synchronous agreement to
  5027. ** asynchronous mode.
  5028. **
  5029. **==========================================================
  5030. */
  5031. static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack)
  5032. {
  5033. struct scsi_cmnd *cmd = cp->cmd;
  5034. u16 target = INB (nc_sdid) & 0x0f;
  5035. struct tcb *tp;
  5036. u_char scntl3;
  5037. u_char sxfer;
  5038. BUG_ON(target != (scmd_id(cmd) & 0xf));
  5039. tp = &np->target[target];
  5040. tp->widedone = wide+1;
  5041. scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
  5042. sxfer = ack ? 0 : tp->sval;
  5043. /*
  5044. ** Stop there if sync/wide parameters are unchanged
  5045. */
  5046. if (tp->sval == sxfer && tp->wval == scntl3) return;
  5047. tp->sval = sxfer;
  5048. tp->wval = scntl3;
  5049. /*
  5050. ** Bells and whistles ;-)
  5051. */
  5052. if (bootverbose >= 2) {
  5053. dev_info(&cmd->device->sdev_target->dev, "WIDE SCSI %sabled.\n",
  5054. (scntl3 & EWS) ? "en" : "dis");
  5055. }
  5056. /*
  5057. ** set actual value and sync_status
  5058. ** patch ALL ccbs of this target.
  5059. */
  5060. ncr_set_sync_wide_status(np, target);
  5061. }
  5062. /*==========================================================
  5063. **
  5064. ** Switch tagged mode for a target.
  5065. **
  5066. **==========================================================
  5067. */
  5068. static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
  5069. {
  5070. unsigned char tn = sdev->id, ln = sdev->lun;
  5071. struct tcb *tp = &np->target[tn];
  5072. struct lcb *lp = tp->lp[ln];
  5073. u_char reqtags, maxdepth;
  5074. /*
  5075. ** Just in case ...
  5076. */
  5077. if ((!tp) || (!lp) || !sdev)
  5078. return;
  5079. /*
  5080. ** If SCSI device queue depth is not yet set, leave here.
  5081. */
  5082. if (!lp->scdev_depth)
  5083. return;
  5084. /*
  5085. ** Donnot allow more tags than the SCSI driver can queue
  5086. ** for this device.
  5087. ** Donnot allow more tags than we can handle.
  5088. */
  5089. maxdepth = lp->scdev_depth;
  5090. if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
  5091. if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
  5092. if (lp->numtags > maxdepth) lp->numtags = maxdepth;
  5093. /*
  5094. ** only devices conformant to ANSI Version >= 2
  5095. ** only devices capable of tagged commands
  5096. ** only if enabled by user ..
  5097. */
  5098. if (sdev->tagged_supported && lp->numtags > 1) {
  5099. reqtags = lp->numtags;
  5100. } else {
  5101. reqtags = 1;
  5102. }
  5103. /*
  5104. ** Update max number of tags
  5105. */
  5106. lp->numtags = reqtags;
  5107. if (lp->numtags > lp->maxtags)
  5108. lp->maxtags = lp->numtags;
  5109. /*
  5110. ** If we want to switch tag mode, we must wait
  5111. ** for no CCB to be active.
  5112. */
  5113. if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
  5114. if (lp->queuedepth == reqtags) /* Already announced */
  5115. return;
  5116. lp->queuedepth = reqtags;
  5117. }
  5118. else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
  5119. lp->queuedepth = reqtags;
  5120. return;
  5121. }
  5122. else { /* Want to switch tag mode */
  5123. if (lp->busyccbs) /* If not yet safe, return */
  5124. return;
  5125. lp->queuedepth = reqtags;
  5126. lp->usetags = reqtags > 1 ? 1 : 0;
  5127. }
  5128. /*
  5129. ** Patch the lun mini-script, according to tag mode.
  5130. */
  5131. lp->jump_tag.l_paddr = lp->usetags?
  5132. cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
  5133. cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
  5134. /*
  5135. ** Announce change to user.
  5136. */
  5137. if (bootverbose) {
  5138. if (lp->usetags) {
  5139. dev_info(&sdev->sdev_gendev,
  5140. "tagged command queue depth set to %d\n",
  5141. reqtags);
  5142. } else {
  5143. dev_info(&sdev->sdev_gendev,
  5144. "tagged command queueing disabled\n");
  5145. }
  5146. }
  5147. }
  5148. /*==========================================================
  5149. **
  5150. **
  5151. ** ncr timeout handler.
  5152. **
  5153. **
  5154. **==========================================================
  5155. **
  5156. ** Misused to keep the driver running when
  5157. ** interrupts are not configured correctly.
  5158. **
  5159. **----------------------------------------------------------
  5160. */
  5161. static void ncr_timeout (struct ncb *np)
  5162. {
  5163. u_long thistime = jiffies;
  5164. /*
  5165. ** If release process in progress, let's go
  5166. ** Set the release stage from 1 to 2 to synchronize
  5167. ** with the release process.
  5168. */
  5169. if (np->release_stage) {
  5170. if (np->release_stage == 1) np->release_stage = 2;
  5171. return;
  5172. }
  5173. np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
  5174. add_timer(&np->timer);
  5175. /*
  5176. ** If we are resetting the ncr, wait for settle_time before
  5177. ** clearing it. Then command processing will be resumed.
  5178. */
  5179. if (np->settle_time) {
  5180. if (np->settle_time <= thistime) {
  5181. if (bootverbose > 1)
  5182. printk("%s: command processing resumed\n", ncr_name(np));
  5183. np->settle_time = 0;
  5184. np->disc = 1;
  5185. requeue_waiting_list(np);
  5186. }
  5187. return;
  5188. }
  5189. /*
  5190. ** Since the generic scsi driver only allows us 0.5 second
  5191. ** to perform abort of a command, we must look at ccbs about
  5192. ** every 0.25 second.
  5193. */
  5194. if (np->lasttime + 4*HZ < thistime) {
  5195. /*
  5196. ** block ncr interrupts
  5197. */
  5198. np->lasttime = thistime;
  5199. }
  5200. #ifdef SCSI_NCR_BROKEN_INTR
  5201. if (INB(nc_istat) & (INTF|SIP|DIP)) {
  5202. /*
  5203. ** Process pending interrupts.
  5204. */
  5205. if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
  5206. ncr_exception (np);
  5207. if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
  5208. }
  5209. #endif /* SCSI_NCR_BROKEN_INTR */
  5210. }
  5211. /*==========================================================
  5212. **
  5213. ** log message for real hard errors
  5214. **
  5215. ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
  5216. ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
  5217. **
  5218. ** exception register:
  5219. ** ds: dstat
  5220. ** si: sist
  5221. **
  5222. ** SCSI bus lines:
  5223. ** so: control lines as driver by NCR.
  5224. ** si: control lines as seen by NCR.
  5225. ** sd: scsi data lines as seen by NCR.
  5226. **
  5227. ** wide/fastmode:
  5228. ** sxfer: (see the manual)
  5229. ** scntl3: (see the manual)
  5230. **
  5231. ** current script command:
  5232. ** dsp: script address (relative to start of script).
  5233. ** dbc: first word of script command.
  5234. **
  5235. ** First 16 register of the chip:
  5236. ** r0..rf
  5237. **
  5238. **==========================================================
  5239. */
  5240. static void ncr_log_hard_error(struct ncb *np, u16 sist, u_char dstat)
  5241. {
  5242. u32 dsp;
  5243. int script_ofs;
  5244. int script_size;
  5245. char *script_name;
  5246. u_char *script_base;
  5247. int i;
  5248. dsp = INL (nc_dsp);
  5249. if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
  5250. script_ofs = dsp - np->p_script;
  5251. script_size = sizeof(struct script);
  5252. script_base = (u_char *) np->script0;
  5253. script_name = "script";
  5254. }
  5255. else if (np->p_scripth < dsp &&
  5256. dsp <= np->p_scripth + sizeof(struct scripth)) {
  5257. script_ofs = dsp - np->p_scripth;
  5258. script_size = sizeof(struct scripth);
  5259. script_base = (u_char *) np->scripth0;
  5260. script_name = "scripth";
  5261. } else {
  5262. script_ofs = dsp;
  5263. script_size = 0;
  5264. script_base = NULL;
  5265. script_name = "mem";
  5266. }
  5267. printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
  5268. ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
  5269. (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
  5270. (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
  5271. (unsigned)INL (nc_dbc));
  5272. if (((script_ofs & 3) == 0) &&
  5273. (unsigned)script_ofs < script_size) {
  5274. printk ("%s: script cmd = %08x\n", ncr_name(np),
  5275. scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
  5276. }
  5277. printk ("%s: regdump:", ncr_name(np));
  5278. for (i=0; i<16;i++)
  5279. printk (" %02x", (unsigned)INB_OFF(i));
  5280. printk (".\n");
  5281. }
  5282. /*============================================================
  5283. **
  5284. ** ncr chip exception handler.
  5285. **
  5286. **============================================================
  5287. **
  5288. ** In normal cases, interrupt conditions occur one at a
  5289. ** time. The ncr is able to stack in some extra registers
  5290. ** other interrupts that will occurs after the first one.
  5291. ** But severall interrupts may occur at the same time.
  5292. **
  5293. ** We probably should only try to deal with the normal
  5294. ** case, but it seems that multiple interrupts occur in
  5295. ** some cases that are not abnormal at all.
  5296. **
  5297. ** The most frequent interrupt condition is Phase Mismatch.
  5298. ** We should want to service this interrupt quickly.
  5299. ** A SCSI parity error may be delivered at the same time.
  5300. ** The SIR interrupt is not very frequent in this driver,
  5301. ** since the INTFLY is likely used for command completion
  5302. ** signaling.
  5303. ** The Selection Timeout interrupt may be triggered with
  5304. ** IID and/or UDC.
  5305. ** The SBMC interrupt (SCSI Bus Mode Change) may probably
  5306. ** occur at any time.
  5307. **
  5308. ** This handler try to deal as cleverly as possible with all
  5309. ** the above.
  5310. **
  5311. **============================================================
  5312. */
  5313. void ncr_exception (struct ncb *np)
  5314. {
  5315. u_char istat, dstat;
  5316. u16 sist;
  5317. int i;
  5318. /*
  5319. ** interrupt on the fly ?
  5320. ** Since the global header may be copied back to a CCB
  5321. ** using a posted PCI memory write, the last operation on
  5322. ** the istat register is a READ in order to flush posted
  5323. ** PCI write commands.
  5324. */
  5325. istat = INB (nc_istat);
  5326. if (istat & INTF) {
  5327. OUTB (nc_istat, (istat & SIGP) | INTF);
  5328. istat = INB (nc_istat);
  5329. if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
  5330. ncr_wakeup_done (np);
  5331. }
  5332. if (!(istat & (SIP|DIP)))
  5333. return;
  5334. if (istat & CABRT)
  5335. OUTB (nc_istat, CABRT);
  5336. /*
  5337. ** Steinbach's Guideline for Systems Programming:
  5338. ** Never test for an error condition you don't know how to handle.
  5339. */
  5340. sist = (istat & SIP) ? INW (nc_sist) : 0;
  5341. dstat = (istat & DIP) ? INB (nc_dstat) : 0;
  5342. if (DEBUG_FLAGS & DEBUG_TINY)
  5343. printk ("<%d|%x:%x|%x:%x>",
  5344. (int)INB(nc_scr0),
  5345. dstat,sist,
  5346. (unsigned)INL(nc_dsp),
  5347. (unsigned)INL(nc_dbc));
  5348. /*========================================================
  5349. ** First, interrupts we want to service cleanly.
  5350. **
  5351. ** Phase mismatch is the most frequent interrupt, and
  5352. ** so we have to service it as quickly and as cleanly
  5353. ** as possible.
  5354. ** Programmed interrupts are rarely used in this driver,
  5355. ** but we must handle them cleanly anyway.
  5356. ** We try to deal with PAR and SBMC combined with
  5357. ** some other interrupt(s).
  5358. **=========================================================
  5359. */
  5360. if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
  5361. !(dstat & (MDPE|BF|ABRT|IID))) {
  5362. if ((sist & SBMC) && ncr_int_sbmc (np))
  5363. return;
  5364. if ((sist & PAR) && ncr_int_par (np))
  5365. return;
  5366. if (sist & MA) {
  5367. ncr_int_ma (np);
  5368. return;
  5369. }
  5370. if (dstat & SIR) {
  5371. ncr_int_sir (np);
  5372. return;
  5373. }
  5374. /*
  5375. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
  5376. */
  5377. if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
  5378. printk( "%s: unknown interrupt(s) ignored, "
  5379. "ISTAT=%x DSTAT=%x SIST=%x\n",
  5380. ncr_name(np), istat, dstat, sist);
  5381. return;
  5382. }
  5383. OUTONB_STD ();
  5384. return;
  5385. }
  5386. /*========================================================
  5387. ** Now, interrupts that need some fixing up.
  5388. ** Order and multiple interrupts is so less important.
  5389. **
  5390. ** If SRST has been asserted, we just reset the chip.
  5391. **
  5392. ** Selection is intirely handled by the chip. If the
  5393. ** chip says STO, we trust it. Seems some other
  5394. ** interrupts may occur at the same time (UDC, IID), so
  5395. ** we ignore them. In any case we do enough fix-up
  5396. ** in the service routine.
  5397. ** We just exclude some fatal dma errors.
  5398. **=========================================================
  5399. */
  5400. if (sist & RST) {
  5401. ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
  5402. return;
  5403. }
  5404. if ((sist & STO) &&
  5405. !(dstat & (MDPE|BF|ABRT))) {
  5406. /*
  5407. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
  5408. */
  5409. OUTONB (nc_ctest3, CLF);
  5410. ncr_int_sto (np);
  5411. return;
  5412. }
  5413. /*=========================================================
  5414. ** Now, interrupts we are not able to recover cleanly.
  5415. ** (At least for the moment).
  5416. **
  5417. ** Do the register dump.
  5418. ** Log message for real hard errors.
  5419. ** Clear all fifos.
  5420. ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
  5421. ** BUS and the chip.
  5422. ** We are more soft for UDC.
  5423. **=========================================================
  5424. */
  5425. if (time_after(jiffies, np->regtime)) {
  5426. np->regtime = jiffies + 10*HZ;
  5427. for (i = 0; i<sizeof(np->regdump); i++)
  5428. ((char*)&np->regdump)[i] = INB_OFF(i);
  5429. np->regdump.nc_dstat = dstat;
  5430. np->regdump.nc_sist = sist;
  5431. }
  5432. ncr_log_hard_error(np, sist, dstat);
  5433. printk ("%s: have to clear fifos.\n", ncr_name (np));
  5434. OUTB (nc_stest3, TE|CSF);
  5435. OUTONB (nc_ctest3, CLF);
  5436. if ((sist & (SGE)) ||
  5437. (dstat & (MDPE|BF|ABRT|IID))) {
  5438. ncr_start_reset(np);
  5439. return;
  5440. }
  5441. if (sist & HTH) {
  5442. printk ("%s: handshake timeout\n", ncr_name(np));
  5443. ncr_start_reset(np);
  5444. return;
  5445. }
  5446. if (sist & UDC) {
  5447. printk ("%s: unexpected disconnect\n", ncr_name(np));
  5448. OUTB (HS_PRT, HS_UNEXPECTED);
  5449. OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
  5450. return;
  5451. }
  5452. /*=========================================================
  5453. ** We just miss the cause of the interrupt. :(
  5454. ** Print a message. The timeout will do the real work.
  5455. **=========================================================
  5456. */
  5457. printk ("%s: unknown interrupt\n", ncr_name(np));
  5458. }
  5459. /*==========================================================
  5460. **
  5461. ** ncr chip exception handler for selection timeout
  5462. **
  5463. **==========================================================
  5464. **
  5465. ** There seems to be a bug in the 53c810.
  5466. ** Although a STO-Interrupt is pending,
  5467. ** it continues executing script commands.
  5468. ** But it will fail and interrupt (IID) on
  5469. ** the next instruction where it's looking
  5470. ** for a valid phase.
  5471. **
  5472. **----------------------------------------------------------
  5473. */
  5474. void ncr_int_sto (struct ncb *np)
  5475. {
  5476. u_long dsa;
  5477. struct ccb *cp;
  5478. if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
  5479. /*
  5480. ** look for ccb and set the status.
  5481. */
  5482. dsa = INL (nc_dsa);
  5483. cp = np->ccb;
  5484. while (cp && (CCB_PHYS (cp, phys) != dsa))
  5485. cp = cp->link_ccb;
  5486. if (cp) {
  5487. cp-> host_status = HS_SEL_TIMEOUT;
  5488. ncr_complete (np, cp);
  5489. }
  5490. /*
  5491. ** repair start queue and jump to start point.
  5492. */
  5493. OUTL_DSP (NCB_SCRIPTH_PHYS (np, sto_restart));
  5494. return;
  5495. }
  5496. /*==========================================================
  5497. **
  5498. ** ncr chip exception handler for SCSI bus mode change
  5499. **
  5500. **==========================================================
  5501. **
  5502. ** spi2-r12 11.2.3 says a transceiver mode change must
  5503. ** generate a reset event and a device that detects a reset
  5504. ** event shall initiate a hard reset. It says also that a
  5505. ** device that detects a mode change shall set data transfer
  5506. ** mode to eight bit asynchronous, etc...
  5507. ** So, just resetting should be enough.
  5508. **
  5509. **
  5510. **----------------------------------------------------------
  5511. */
  5512. static int ncr_int_sbmc (struct ncb *np)
  5513. {
  5514. u_char scsi_mode = INB (nc_stest4) & SMODE;
  5515. if (scsi_mode != np->scsi_mode) {
  5516. printk("%s: SCSI bus mode change from %x to %x.\n",
  5517. ncr_name(np), np->scsi_mode, scsi_mode);
  5518. np->scsi_mode = scsi_mode;
  5519. /*
  5520. ** Suspend command processing for 1 second and
  5521. ** reinitialize all except the chip.
  5522. */
  5523. np->settle_time = jiffies + HZ;
  5524. ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
  5525. return 1;
  5526. }
  5527. return 0;
  5528. }
  5529. /*==========================================================
  5530. **
  5531. ** ncr chip exception handler for SCSI parity error.
  5532. **
  5533. **==========================================================
  5534. **
  5535. **
  5536. **----------------------------------------------------------
  5537. */
  5538. static int ncr_int_par (struct ncb *np)
  5539. {
  5540. u_char hsts = INB (HS_PRT);
  5541. u32 dbc = INL (nc_dbc);
  5542. u_char sstat1 = INB (nc_sstat1);
  5543. int phase = -1;
  5544. int msg = -1;
  5545. u32 jmp;
  5546. printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
  5547. ncr_name(np), hsts, dbc, sstat1);
  5548. /*
  5549. * Ignore the interrupt if the NCR is not connected
  5550. * to the SCSI bus, since the right work should have
  5551. * been done on unexpected disconnection handling.
  5552. */
  5553. if (!(INB (nc_scntl1) & ISCON))
  5554. return 0;
  5555. /*
  5556. * If the nexus is not clearly identified, reset the bus.
  5557. * We will try to do better later.
  5558. */
  5559. if (hsts & HS_INVALMASK)
  5560. goto reset_all;
  5561. /*
  5562. * If the SCSI parity error occurs in MSG IN phase, prepare a
  5563. * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
  5564. * ERROR message and let the device decide to retry the command
  5565. * or to terminate with check condition. If we were in MSG IN
  5566. * phase waiting for the response of a negotiation, we will
  5567. * get SIR_NEGO_FAILED at dispatch.
  5568. */
  5569. if (!(dbc & 0xc0000000))
  5570. phase = (dbc >> 24) & 7;
  5571. if (phase == 7)
  5572. msg = MSG_PARITY_ERROR;
  5573. else
  5574. msg = INITIATOR_ERROR;
  5575. /*
  5576. * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
  5577. * script that will ignore all data in bytes until phase
  5578. * change, since we are not sure the chip will wait the phase
  5579. * change prior to delivering the interrupt.
  5580. */
  5581. if (phase == 1)
  5582. jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
  5583. else
  5584. jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
  5585. OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
  5586. OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
  5587. np->msgout[0] = msg;
  5588. OUTL_DSP (jmp);
  5589. return 1;
  5590. reset_all:
  5591. ncr_start_reset(np);
  5592. return 1;
  5593. }
  5594. /*==========================================================
  5595. **
  5596. **
  5597. ** ncr chip exception handler for phase errors.
  5598. **
  5599. **
  5600. **==========================================================
  5601. **
  5602. ** We have to construct a new transfer descriptor,
  5603. ** to transfer the rest of the current block.
  5604. **
  5605. **----------------------------------------------------------
  5606. */
  5607. static void ncr_int_ma (struct ncb *np)
  5608. {
  5609. u32 dbc;
  5610. u32 rest;
  5611. u32 dsp;
  5612. u32 dsa;
  5613. u32 nxtdsp;
  5614. u32 newtmp;
  5615. u32 *vdsp;
  5616. u32 oadr, olen;
  5617. u32 *tblp;
  5618. ncrcmd *newcmd;
  5619. u_char cmd, sbcl;
  5620. struct ccb *cp;
  5621. dsp = INL (nc_dsp);
  5622. dbc = INL (nc_dbc);
  5623. sbcl = INB (nc_sbcl);
  5624. cmd = dbc >> 24;
  5625. rest = dbc & 0xffffff;
  5626. /*
  5627. ** Take into account dma fifo and various buffers and latches,
  5628. ** only if the interrupted phase is an OUTPUT phase.
  5629. */
  5630. if ((cmd & 1) == 0) {
  5631. u_char ctest5, ss0, ss2;
  5632. u16 delta;
  5633. ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
  5634. if (ctest5 & DFS)
  5635. delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
  5636. else
  5637. delta=(INB (nc_dfifo) - rest) & 0x7f;
  5638. /*
  5639. ** The data in the dma fifo has not been transferred to
  5640. ** the target -> add the amount to the rest
  5641. ** and clear the data.
  5642. ** Check the sstat2 register in case of wide transfer.
  5643. */
  5644. rest += delta;
  5645. ss0 = INB (nc_sstat0);
  5646. if (ss0 & OLF) rest++;
  5647. if (ss0 & ORF) rest++;
  5648. if (INB(nc_scntl3) & EWS) {
  5649. ss2 = INB (nc_sstat2);
  5650. if (ss2 & OLF1) rest++;
  5651. if (ss2 & ORF1) rest++;
  5652. }
  5653. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  5654. printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
  5655. (unsigned) rest, (unsigned) delta, ss0);
  5656. } else {
  5657. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  5658. printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
  5659. }
  5660. /*
  5661. ** Clear fifos.
  5662. */
  5663. OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
  5664. OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
  5665. /*
  5666. ** locate matching cp.
  5667. ** if the interrupted phase is DATA IN or DATA OUT,
  5668. ** trust the global header.
  5669. */
  5670. dsa = INL (nc_dsa);
  5671. if (!(cmd & 6)) {
  5672. cp = np->header.cp;
  5673. if (CCB_PHYS(cp, phys) != dsa)
  5674. cp = NULL;
  5675. } else {
  5676. cp = np->ccb;
  5677. while (cp && (CCB_PHYS (cp, phys) != dsa))
  5678. cp = cp->link_ccb;
  5679. }
  5680. /*
  5681. ** try to find the interrupted script command,
  5682. ** and the address at which to continue.
  5683. */
  5684. vdsp = NULL;
  5685. nxtdsp = 0;
  5686. if (dsp > np->p_script &&
  5687. dsp <= np->p_script + sizeof(struct script)) {
  5688. vdsp = (u32 *)((char*)np->script0 + (dsp-np->p_script-8));
  5689. nxtdsp = dsp;
  5690. }
  5691. else if (dsp > np->p_scripth &&
  5692. dsp <= np->p_scripth + sizeof(struct scripth)) {
  5693. vdsp = (u32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
  5694. nxtdsp = dsp;
  5695. }
  5696. else if (cp) {
  5697. if (dsp == CCB_PHYS (cp, patch[2])) {
  5698. vdsp = &cp->patch[0];
  5699. nxtdsp = scr_to_cpu(vdsp[3]);
  5700. }
  5701. else if (dsp == CCB_PHYS (cp, patch[6])) {
  5702. vdsp = &cp->patch[4];
  5703. nxtdsp = scr_to_cpu(vdsp[3]);
  5704. }
  5705. }
  5706. /*
  5707. ** log the information
  5708. */
  5709. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5710. printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
  5711. cp, np->header.cp,
  5712. (unsigned)dsp,
  5713. (unsigned)nxtdsp, vdsp, cmd);
  5714. }
  5715. /*
  5716. ** cp=0 means that the DSA does not point to a valid control
  5717. ** block. This should not happen since we donnot use multi-byte
  5718. ** move while we are being reselected ot after command complete.
  5719. ** We are not able to recover from such a phase error.
  5720. */
  5721. if (!cp) {
  5722. printk ("%s: SCSI phase error fixup: "
  5723. "CCB already dequeued (0x%08lx)\n",
  5724. ncr_name (np), (u_long) np->header.cp);
  5725. goto reset_all;
  5726. }
  5727. /*
  5728. ** get old startaddress and old length.
  5729. */
  5730. oadr = scr_to_cpu(vdsp[1]);
  5731. if (cmd & 0x10) { /* Table indirect */
  5732. tblp = (u32 *) ((char*) &cp->phys + oadr);
  5733. olen = scr_to_cpu(tblp[0]);
  5734. oadr = scr_to_cpu(tblp[1]);
  5735. } else {
  5736. tblp = (u32 *) 0;
  5737. olen = scr_to_cpu(vdsp[0]) & 0xffffff;
  5738. }
  5739. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5740. printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
  5741. (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
  5742. tblp,
  5743. (unsigned) olen,
  5744. (unsigned) oadr);
  5745. }
  5746. /*
  5747. ** check cmd against assumed interrupted script command.
  5748. */
  5749. if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
  5750. PRINT_ADDR(cp->cmd, "internal error: cmd=%02x != %02x=(vdsp[0] "
  5751. ">> 24)\n", cmd, scr_to_cpu(vdsp[0]) >> 24);
  5752. goto reset_all;
  5753. }
  5754. /*
  5755. ** cp != np->header.cp means that the header of the CCB
  5756. ** currently being processed has not yet been copied to
  5757. ** the global header area. That may happen if the device did
  5758. ** not accept all our messages after having been selected.
  5759. */
  5760. if (cp != np->header.cp) {
  5761. printk ("%s: SCSI phase error fixup: "
  5762. "CCB address mismatch (0x%08lx != 0x%08lx)\n",
  5763. ncr_name (np), (u_long) cp, (u_long) np->header.cp);
  5764. }
  5765. /*
  5766. ** if old phase not dataphase, leave here.
  5767. */
  5768. if (cmd & 0x06) {
  5769. PRINT_ADDR(cp->cmd, "phase change %x-%x %d@%08x resid=%d.\n",
  5770. cmd&7, sbcl&7, (unsigned)olen,
  5771. (unsigned)oadr, (unsigned)rest);
  5772. goto unexpected_phase;
  5773. }
  5774. /*
  5775. ** choose the correct patch area.
  5776. ** if savep points to one, choose the other.
  5777. */
  5778. newcmd = cp->patch;
  5779. newtmp = CCB_PHYS (cp, patch);
  5780. if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
  5781. newcmd = &cp->patch[4];
  5782. newtmp = CCB_PHYS (cp, patch[4]);
  5783. }
  5784. /*
  5785. ** fillin the commands
  5786. */
  5787. newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
  5788. newcmd[1] = cpu_to_scr(oadr + olen - rest);
  5789. newcmd[2] = cpu_to_scr(SCR_JUMP);
  5790. newcmd[3] = cpu_to_scr(nxtdsp);
  5791. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5792. PRINT_ADDR(cp->cmd, "newcmd[%d] %x %x %x %x.\n",
  5793. (int) (newcmd - cp->patch),
  5794. (unsigned)scr_to_cpu(newcmd[0]),
  5795. (unsigned)scr_to_cpu(newcmd[1]),
  5796. (unsigned)scr_to_cpu(newcmd[2]),
  5797. (unsigned)scr_to_cpu(newcmd[3]));
  5798. }
  5799. /*
  5800. ** fake the return address (to the patch).
  5801. ** and restart script processor at dispatcher.
  5802. */
  5803. OUTL (nc_temp, newtmp);
  5804. OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
  5805. return;
  5806. /*
  5807. ** Unexpected phase changes that occurs when the current phase
  5808. ** is not a DATA IN or DATA OUT phase are due to error conditions.
  5809. ** Such event may only happen when the SCRIPTS is using a
  5810. ** multibyte SCSI MOVE.
  5811. **
  5812. ** Phase change Some possible cause
  5813. **
  5814. ** COMMAND --> MSG IN SCSI parity error detected by target.
  5815. ** COMMAND --> STATUS Bad command or refused by target.
  5816. ** MSG OUT --> MSG IN Message rejected by target.
  5817. ** MSG OUT --> COMMAND Bogus target that discards extended
  5818. ** negotiation messages.
  5819. **
  5820. ** The code below does not care of the new phase and so
  5821. ** trusts the target. Why to annoy it ?
  5822. ** If the interrupted phase is COMMAND phase, we restart at
  5823. ** dispatcher.
  5824. ** If a target does not get all the messages after selection,
  5825. ** the code assumes blindly that the target discards extended
  5826. ** messages and clears the negotiation status.
  5827. ** If the target does not want all our response to negotiation,
  5828. ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
  5829. ** bloat for such a should_not_happen situation).
  5830. ** In all other situation, we reset the BUS.
  5831. ** Are these assumptions reasonnable ? (Wait and see ...)
  5832. */
  5833. unexpected_phase:
  5834. dsp -= 8;
  5835. nxtdsp = 0;
  5836. switch (cmd & 7) {
  5837. case 2: /* COMMAND phase */
  5838. nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
  5839. break;
  5840. #if 0
  5841. case 3: /* STATUS phase */
  5842. nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
  5843. break;
  5844. #endif
  5845. case 6: /* MSG OUT phase */
  5846. np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
  5847. if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
  5848. cp->host_status = HS_BUSY;
  5849. nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
  5850. }
  5851. else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
  5852. dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
  5853. nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
  5854. }
  5855. break;
  5856. #if 0
  5857. case 7: /* MSG IN phase */
  5858. nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
  5859. break;
  5860. #endif
  5861. }
  5862. if (nxtdsp) {
  5863. OUTL_DSP (nxtdsp);
  5864. return;
  5865. }
  5866. reset_all:
  5867. ncr_start_reset(np);
  5868. }
  5869. static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
  5870. {
  5871. struct scsi_cmnd *cmd = cp->cmd;
  5872. struct tcb *tp = &np->target[cmd->device->id];
  5873. struct lcb *lp = tp->lp[cmd->device->lun];
  5874. struct list_head *qp;
  5875. struct ccb * cp2;
  5876. int disc_cnt = 0;
  5877. int busy_cnt = 0;
  5878. u32 startp;
  5879. u_char s_status = INB (SS_PRT);
  5880. /*
  5881. ** Let the SCRIPTS processor skip all not yet started CCBs,
  5882. ** and count disconnected CCBs. Since the busy queue is in
  5883. ** the same order as the chip start queue, disconnected CCBs
  5884. ** are before cp and busy ones after.
  5885. */
  5886. if (lp) {
  5887. qp = lp->busy_ccbq.prev;
  5888. while (qp != &lp->busy_ccbq) {
  5889. cp2 = list_entry(qp, struct ccb, link_ccbq);
  5890. qp = qp->prev;
  5891. ++busy_cnt;
  5892. if (cp2 == cp)
  5893. break;
  5894. cp2->start.schedule.l_paddr =
  5895. cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
  5896. }
  5897. lp->held_ccb = cp; /* Requeue when this one completes */
  5898. disc_cnt = lp->queuedccbs - busy_cnt;
  5899. }
  5900. switch(s_status) {
  5901. default: /* Just for safety, should never happen */
  5902. case S_QUEUE_FULL:
  5903. /*
  5904. ** Decrease number of tags to the number of
  5905. ** disconnected commands.
  5906. */
  5907. if (!lp)
  5908. goto out;
  5909. if (bootverbose >= 1) {
  5910. PRINT_ADDR(cmd, "QUEUE FULL! %d busy, %d disconnected "
  5911. "CCBs\n", busy_cnt, disc_cnt);
  5912. }
  5913. if (disc_cnt < lp->numtags) {
  5914. lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
  5915. lp->num_good = 0;
  5916. ncr_setup_tags (np, cmd->device);
  5917. }
  5918. /*
  5919. ** Requeue the command to the start queue.
  5920. ** If any disconnected commands,
  5921. ** Clear SIGP.
  5922. ** Jump to reselect.
  5923. */
  5924. cp->phys.header.savep = cp->startp;
  5925. cp->host_status = HS_BUSY;
  5926. cp->scsi_status = S_ILLEGAL;
  5927. ncr_put_start_queue(np, cp);
  5928. if (disc_cnt)
  5929. INB (nc_ctest2); /* Clear SIGP */
  5930. OUTL_DSP (NCB_SCRIPT_PHYS (np, reselect));
  5931. return;
  5932. case S_TERMINATED:
  5933. case S_CHECK_COND:
  5934. /*
  5935. ** If we were requesting sense, give up.
  5936. */
  5937. if (cp->auto_sense)
  5938. goto out;
  5939. /*
  5940. ** Device returned CHECK CONDITION status.
  5941. ** Prepare all needed data strutures for getting
  5942. ** sense data.
  5943. **
  5944. ** identify message
  5945. */
  5946. cp->scsi_smsg2[0] = IDENTIFY(0, cmd->device->lun);
  5947. cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
  5948. cp->phys.smsg.size = cpu_to_scr(1);
  5949. /*
  5950. ** sense command
  5951. */
  5952. cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
  5953. cp->phys.cmd.size = cpu_to_scr(6);
  5954. /*
  5955. ** patch requested size into sense command
  5956. */
  5957. cp->sensecmd[0] = 0x03;
  5958. cp->sensecmd[1] = cmd->device->lun << 5;
  5959. cp->sensecmd[4] = sizeof(cp->sense_buf);
  5960. /*
  5961. ** sense data
  5962. */
  5963. memset(cp->sense_buf, 0, sizeof(cp->sense_buf));
  5964. cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
  5965. cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
  5966. /*
  5967. ** requeue the command.
  5968. */
  5969. startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
  5970. cp->phys.header.savep = startp;
  5971. cp->phys.header.goalp = startp + 24;
  5972. cp->phys.header.lastp = startp;
  5973. cp->phys.header.wgoalp = startp + 24;
  5974. cp->phys.header.wlastp = startp;
  5975. cp->host_status = HS_BUSY;
  5976. cp->scsi_status = S_ILLEGAL;
  5977. cp->auto_sense = s_status;
  5978. cp->start.schedule.l_paddr =
  5979. cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  5980. /*
  5981. ** Select without ATN for quirky devices.
  5982. */
  5983. if (cmd->device->select_no_atn)
  5984. cp->start.schedule.l_paddr =
  5985. cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
  5986. ncr_put_start_queue(np, cp);
  5987. OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
  5988. return;
  5989. }
  5990. out:
  5991. OUTONB_STD ();
  5992. return;
  5993. }
  5994. /*==========================================================
  5995. **
  5996. **
  5997. ** ncr chip exception handler for programmed interrupts.
  5998. **
  5999. **
  6000. **==========================================================
  6001. */
  6002. void ncr_int_sir (struct ncb *np)
  6003. {
  6004. u_char scntl3;
  6005. u_char chg, ofs, per, fak, wide;
  6006. u_char num = INB (nc_dsps);
  6007. struct ccb *cp=NULL;
  6008. u_long dsa = INL (nc_dsa);
  6009. u_char target = INB (nc_sdid) & 0x0f;
  6010. struct tcb *tp = &np->target[target];
  6011. struct scsi_target *starget = tp->starget;
  6012. if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
  6013. switch (num) {
  6014. case SIR_INTFLY:
  6015. /*
  6016. ** This is used for HP Zalon/53c720 where INTFLY
  6017. ** operation is currently broken.
  6018. */
  6019. ncr_wakeup_done(np);
  6020. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  6021. OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, done_end) + 8);
  6022. #else
  6023. OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, start));
  6024. #endif
  6025. return;
  6026. case SIR_RESEL_NO_MSG_IN:
  6027. case SIR_RESEL_NO_IDENTIFY:
  6028. /*
  6029. ** If devices reselecting without sending an IDENTIFY
  6030. ** message still exist, this should help.
  6031. ** We just assume lun=0, 1 CCB, no tag.
  6032. */
  6033. if (tp->lp[0]) {
  6034. OUTL_DSP (scr_to_cpu(tp->lp[0]->jump_ccb[0]));
  6035. return;
  6036. }
  6037. case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
  6038. case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
  6039. case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
  6040. case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
  6041. printk ("%s:%d: SIR %d, "
  6042. "incorrect nexus identification on reselection\n",
  6043. ncr_name (np), target, num);
  6044. goto out;
  6045. case SIR_DONE_OVERFLOW:
  6046. printk ("%s:%d: SIR %d, "
  6047. "CCB done queue overflow\n",
  6048. ncr_name (np), target, num);
  6049. goto out;
  6050. case SIR_BAD_STATUS:
  6051. cp = np->header.cp;
  6052. if (!cp || CCB_PHYS (cp, phys) != dsa)
  6053. goto out;
  6054. ncr_sir_to_redo(np, num, cp);
  6055. return;
  6056. default:
  6057. /*
  6058. ** lookup the ccb
  6059. */
  6060. cp = np->ccb;
  6061. while (cp && (CCB_PHYS (cp, phys) != dsa))
  6062. cp = cp->link_ccb;
  6063. BUG_ON(!cp);
  6064. BUG_ON(cp != np->header.cp);
  6065. if (!cp || cp != np->header.cp)
  6066. goto out;
  6067. }
  6068. switch (num) {
  6069. /*-----------------------------------------------------------------------------
  6070. **
  6071. ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
  6072. ** ("Everything you've always wanted to know about transfer mode
  6073. ** negotiation")
  6074. **
  6075. ** We try to negotiate sync and wide transfer only after
  6076. ** a successful inquire command. We look at byte 7 of the
  6077. ** inquire data to determine the capabilities of the target.
  6078. **
  6079. ** When we try to negotiate, we append the negotiation message
  6080. ** to the identify and (maybe) simple tag message.
  6081. ** The host status field is set to HS_NEGOTIATE to mark this
  6082. ** situation.
  6083. **
  6084. ** If the target doesn't answer this message immidiately
  6085. ** (as required by the standard), the SIR_NEGO_FAIL interrupt
  6086. ** will be raised eventually.
  6087. ** The handler removes the HS_NEGOTIATE status, and sets the
  6088. ** negotiated value to the default (async / nowide).
  6089. **
  6090. ** If we receive a matching answer immediately, we check it
  6091. ** for validity, and set the values.
  6092. **
  6093. ** If we receive a Reject message immediately, we assume the
  6094. ** negotiation has failed, and fall back to standard values.
  6095. **
  6096. ** If we receive a negotiation message while not in HS_NEGOTIATE
  6097. ** state, it's a target initiated negotiation. We prepare a
  6098. ** (hopefully) valid answer, set our parameters, and send back
  6099. ** this answer to the target.
  6100. **
  6101. ** If the target doesn't fetch the answer (no message out phase),
  6102. ** we assume the negotiation has failed, and fall back to default
  6103. ** settings.
  6104. **
  6105. ** When we set the values, we adjust them in all ccbs belonging
  6106. ** to this target, in the controller's register, and in the "phys"
  6107. ** field of the controller's struct ncb.
  6108. **
  6109. ** Possible cases: hs sir msg_in value send goto
  6110. ** We try to negotiate:
  6111. ** -> target doesn't msgin NEG FAIL noop defa. - dispatch
  6112. ** -> target rejected our msg NEG FAIL reject defa. - dispatch
  6113. ** -> target answered (ok) NEG SYNC sdtr set - clrack
  6114. ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
  6115. ** -> target answered (ok) NEG WIDE wdtr set - clrack
  6116. ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
  6117. ** -> any other msgin NEG FAIL noop defa. - dispatch
  6118. **
  6119. ** Target tries to negotiate:
  6120. ** -> incoming message --- SYNC sdtr set SDTR -
  6121. ** -> incoming message --- WIDE wdtr set WDTR -
  6122. ** We sent our answer:
  6123. ** -> target doesn't msgout --- PROTO ? defa. - dispatch
  6124. **
  6125. **-----------------------------------------------------------------------------
  6126. */
  6127. case SIR_NEGO_FAILED:
  6128. /*-------------------------------------------------------
  6129. **
  6130. ** Negotiation failed.
  6131. ** Target doesn't send an answer message,
  6132. ** or target rejected our message.
  6133. **
  6134. ** Remove negotiation request.
  6135. **
  6136. **-------------------------------------------------------
  6137. */
  6138. OUTB (HS_PRT, HS_BUSY);
  6139. /* fall through */
  6140. case SIR_NEGO_PROTO:
  6141. /*-------------------------------------------------------
  6142. **
  6143. ** Negotiation failed.
  6144. ** Target doesn't fetch the answer message.
  6145. **
  6146. **-------------------------------------------------------
  6147. */
  6148. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6149. PRINT_ADDR(cp->cmd, "negotiation failed sir=%x "
  6150. "status=%x.\n", num, cp->nego_status);
  6151. }
  6152. /*
  6153. ** any error in negotiation:
  6154. ** fall back to default mode.
  6155. */
  6156. switch (cp->nego_status) {
  6157. case NS_SYNC:
  6158. spi_period(starget) = 0;
  6159. spi_offset(starget) = 0;
  6160. ncr_setsync (np, cp, 0, 0xe0);
  6161. break;
  6162. case NS_WIDE:
  6163. spi_width(starget) = 0;
  6164. ncr_setwide (np, cp, 0, 0);
  6165. break;
  6166. }
  6167. np->msgin [0] = NOP;
  6168. np->msgout[0] = NOP;
  6169. cp->nego_status = 0;
  6170. break;
  6171. case SIR_NEGO_SYNC:
  6172. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6173. ncr_print_msg(cp, "sync msgin", np->msgin);
  6174. }
  6175. chg = 0;
  6176. per = np->msgin[3];
  6177. ofs = np->msgin[4];
  6178. if (ofs==0) per=255;
  6179. /*
  6180. ** if target sends SDTR message,
  6181. ** it CAN transfer synch.
  6182. */
  6183. if (ofs && starget)
  6184. spi_support_sync(starget) = 1;
  6185. /*
  6186. ** check values against driver limits.
  6187. */
  6188. if (per < np->minsync)
  6189. {chg = 1; per = np->minsync;}
  6190. if (per < tp->minsync)
  6191. {chg = 1; per = tp->minsync;}
  6192. if (ofs > tp->maxoffs)
  6193. {chg = 1; ofs = tp->maxoffs;}
  6194. /*
  6195. ** Check against controller limits.
  6196. */
  6197. fak = 7;
  6198. scntl3 = 0;
  6199. if (ofs != 0) {
  6200. ncr_getsync(np, per, &fak, &scntl3);
  6201. if (fak > 7) {
  6202. chg = 1;
  6203. ofs = 0;
  6204. }
  6205. }
  6206. if (ofs == 0) {
  6207. fak = 7;
  6208. per = 0;
  6209. scntl3 = 0;
  6210. tp->minsync = 0;
  6211. }
  6212. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6213. PRINT_ADDR(cp->cmd, "sync: per=%d scntl3=0x%x ofs=%d "
  6214. "fak=%d chg=%d.\n", per, scntl3, ofs, fak, chg);
  6215. }
  6216. if (INB (HS_PRT) == HS_NEGOTIATE) {
  6217. OUTB (HS_PRT, HS_BUSY);
  6218. switch (cp->nego_status) {
  6219. case NS_SYNC:
  6220. /* This was an answer message */
  6221. if (chg) {
  6222. /* Answer wasn't acceptable. */
  6223. spi_period(starget) = 0;
  6224. spi_offset(starget) = 0;
  6225. ncr_setsync(np, cp, 0, 0xe0);
  6226. OUTL_DSP(NCB_SCRIPT_PHYS (np, msg_bad));
  6227. } else {
  6228. /* Answer is ok. */
  6229. spi_period(starget) = per;
  6230. spi_offset(starget) = ofs;
  6231. ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
  6232. OUTL_DSP(NCB_SCRIPT_PHYS (np, clrack));
  6233. }
  6234. return;
  6235. case NS_WIDE:
  6236. spi_width(starget) = 0;
  6237. ncr_setwide(np, cp, 0, 0);
  6238. break;
  6239. }
  6240. }
  6241. /*
  6242. ** It was a request. Set value and
  6243. ** prepare an answer message
  6244. */
  6245. spi_period(starget) = per;
  6246. spi_offset(starget) = ofs;
  6247. ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
  6248. spi_populate_sync_msg(np->msgout, per, ofs);
  6249. cp->nego_status = NS_SYNC;
  6250. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6251. ncr_print_msg(cp, "sync msgout", np->msgout);
  6252. }
  6253. if (!ofs) {
  6254. OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
  6255. return;
  6256. }
  6257. np->msgin [0] = NOP;
  6258. break;
  6259. case SIR_NEGO_WIDE:
  6260. /*
  6261. ** Wide request message received.
  6262. */
  6263. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6264. ncr_print_msg(cp, "wide msgin", np->msgin);
  6265. }
  6266. /*
  6267. ** get requested values.
  6268. */
  6269. chg = 0;
  6270. wide = np->msgin[3];
  6271. /*
  6272. ** if target sends WDTR message,
  6273. ** it CAN transfer wide.
  6274. */
  6275. if (wide && starget)
  6276. spi_support_wide(starget) = 1;
  6277. /*
  6278. ** check values against driver limits.
  6279. */
  6280. if (wide > tp->usrwide)
  6281. {chg = 1; wide = tp->usrwide;}
  6282. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6283. PRINT_ADDR(cp->cmd, "wide: wide=%d chg=%d.\n", wide,
  6284. chg);
  6285. }
  6286. if (INB (HS_PRT) == HS_NEGOTIATE) {
  6287. OUTB (HS_PRT, HS_BUSY);
  6288. switch (cp->nego_status) {
  6289. case NS_WIDE:
  6290. /*
  6291. ** This was an answer message
  6292. */
  6293. if (chg) {
  6294. /* Answer wasn't acceptable. */
  6295. spi_width(starget) = 0;
  6296. ncr_setwide(np, cp, 0, 1);
  6297. OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
  6298. } else {
  6299. /* Answer is ok. */
  6300. spi_width(starget) = wide;
  6301. ncr_setwide(np, cp, wide, 1);
  6302. OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
  6303. }
  6304. return;
  6305. case NS_SYNC:
  6306. spi_period(starget) = 0;
  6307. spi_offset(starget) = 0;
  6308. ncr_setsync(np, cp, 0, 0xe0);
  6309. break;
  6310. }
  6311. }
  6312. /*
  6313. ** It was a request, set value and
  6314. ** prepare an answer message
  6315. */
  6316. spi_width(starget) = wide;
  6317. ncr_setwide(np, cp, wide, 1);
  6318. spi_populate_width_msg(np->msgout, wide);
  6319. np->msgin [0] = NOP;
  6320. cp->nego_status = NS_WIDE;
  6321. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6322. ncr_print_msg(cp, "wide msgout", np->msgin);
  6323. }
  6324. break;
  6325. /*--------------------------------------------------------------------
  6326. **
  6327. ** Processing of special messages
  6328. **
  6329. **--------------------------------------------------------------------
  6330. */
  6331. case SIR_REJECT_RECEIVED:
  6332. /*-----------------------------------------------
  6333. **
  6334. ** We received a MESSAGE_REJECT.
  6335. **
  6336. **-----------------------------------------------
  6337. */
  6338. PRINT_ADDR(cp->cmd, "MESSAGE_REJECT received (%x:%x).\n",
  6339. (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
  6340. break;
  6341. case SIR_REJECT_SENT:
  6342. /*-----------------------------------------------
  6343. **
  6344. ** We received an unknown message
  6345. **
  6346. **-----------------------------------------------
  6347. */
  6348. ncr_print_msg(cp, "MESSAGE_REJECT sent for", np->msgin);
  6349. break;
  6350. /*--------------------------------------------------------------------
  6351. **
  6352. ** Processing of special messages
  6353. **
  6354. **--------------------------------------------------------------------
  6355. */
  6356. case SIR_IGN_RESIDUE:
  6357. /*-----------------------------------------------
  6358. **
  6359. ** We received an IGNORE RESIDUE message,
  6360. ** which couldn't be handled by the script.
  6361. **
  6362. **-----------------------------------------------
  6363. */
  6364. PRINT_ADDR(cp->cmd, "IGNORE_WIDE_RESIDUE received, but not yet "
  6365. "implemented.\n");
  6366. break;
  6367. #if 0
  6368. case SIR_MISSING_SAVE:
  6369. /*-----------------------------------------------
  6370. **
  6371. ** We received an DISCONNECT message,
  6372. ** but the datapointer wasn't saved before.
  6373. **
  6374. **-----------------------------------------------
  6375. */
  6376. PRINT_ADDR(cp->cmd, "DISCONNECT received, but datapointer "
  6377. "not saved: data=%x save=%x goal=%x.\n",
  6378. (unsigned) INL (nc_temp),
  6379. (unsigned) scr_to_cpu(np->header.savep),
  6380. (unsigned) scr_to_cpu(np->header.goalp));
  6381. break;
  6382. #endif
  6383. }
  6384. out:
  6385. OUTONB_STD ();
  6386. }
  6387. /*==========================================================
  6388. **
  6389. **
  6390. ** Acquire a control block
  6391. **
  6392. **
  6393. **==========================================================
  6394. */
  6395. static struct ccb *ncr_get_ccb(struct ncb *np, struct scsi_cmnd *cmd)
  6396. {
  6397. u_char tn = cmd->device->id;
  6398. u_char ln = cmd->device->lun;
  6399. struct tcb *tp = &np->target[tn];
  6400. struct lcb *lp = tp->lp[ln];
  6401. u_char tag = NO_TAG;
  6402. struct ccb *cp = NULL;
  6403. /*
  6404. ** Lun structure available ?
  6405. */
  6406. if (lp) {
  6407. struct list_head *qp;
  6408. /*
  6409. ** Keep from using more tags than we can handle.
  6410. */
  6411. if (lp->usetags && lp->busyccbs >= lp->maxnxs)
  6412. return NULL;
  6413. /*
  6414. ** Allocate a new CCB if needed.
  6415. */
  6416. if (list_empty(&lp->free_ccbq))
  6417. ncr_alloc_ccb(np, tn, ln);
  6418. /*
  6419. ** Look for free CCB
  6420. */
  6421. qp = ncr_list_pop(&lp->free_ccbq);
  6422. if (qp) {
  6423. cp = list_entry(qp, struct ccb, link_ccbq);
  6424. if (cp->magic) {
  6425. PRINT_ADDR(cmd, "ccb free list corrupted "
  6426. "(@%p)\n", cp);
  6427. cp = NULL;
  6428. } else {
  6429. list_add_tail(qp, &lp->wait_ccbq);
  6430. ++lp->busyccbs;
  6431. }
  6432. }
  6433. /*
  6434. ** If a CCB is available,
  6435. ** Get a tag for this nexus if required.
  6436. */
  6437. if (cp) {
  6438. if (lp->usetags)
  6439. tag = lp->cb_tags[lp->ia_tag];
  6440. }
  6441. else if (lp->actccbs > 0)
  6442. return NULL;
  6443. }
  6444. /*
  6445. ** if nothing available, take the default.
  6446. */
  6447. if (!cp)
  6448. cp = np->ccb;
  6449. /*
  6450. ** Wait until available.
  6451. */
  6452. #if 0
  6453. while (cp->magic) {
  6454. if (flags & SCSI_NOSLEEP) break;
  6455. if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
  6456. break;
  6457. }
  6458. #endif
  6459. if (cp->magic)
  6460. return NULL;
  6461. cp->magic = 1;
  6462. /*
  6463. ** Move to next available tag if tag used.
  6464. */
  6465. if (lp) {
  6466. if (tag != NO_TAG) {
  6467. ++lp->ia_tag;
  6468. if (lp->ia_tag == MAX_TAGS)
  6469. lp->ia_tag = 0;
  6470. lp->tags_umap |= (((tagmap_t) 1) << tag);
  6471. }
  6472. }
  6473. /*
  6474. ** Remember all informations needed to free this CCB.
  6475. */
  6476. cp->tag = tag;
  6477. cp->target = tn;
  6478. cp->lun = ln;
  6479. if (DEBUG_FLAGS & DEBUG_TAGS) {
  6480. PRINT_ADDR(cmd, "ccb @%p using tag %d.\n", cp, tag);
  6481. }
  6482. return cp;
  6483. }
  6484. /*==========================================================
  6485. **
  6486. **
  6487. ** Release one control block
  6488. **
  6489. **
  6490. **==========================================================
  6491. */
  6492. static void ncr_free_ccb (struct ncb *np, struct ccb *cp)
  6493. {
  6494. struct tcb *tp = &np->target[cp->target];
  6495. struct lcb *lp = tp->lp[cp->lun];
  6496. if (DEBUG_FLAGS & DEBUG_TAGS) {
  6497. PRINT_ADDR(cp->cmd, "ccb @%p freeing tag %d.\n", cp, cp->tag);
  6498. }
  6499. /*
  6500. ** If lun control block available,
  6501. ** decrement active commands and increment credit,
  6502. ** free the tag if any and remove the JUMP for reselect.
  6503. */
  6504. if (lp) {
  6505. if (cp->tag != NO_TAG) {
  6506. lp->cb_tags[lp->if_tag++] = cp->tag;
  6507. if (lp->if_tag == MAX_TAGS)
  6508. lp->if_tag = 0;
  6509. lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
  6510. lp->tags_smap &= lp->tags_umap;
  6511. lp->jump_ccb[cp->tag] =
  6512. cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
  6513. } else {
  6514. lp->jump_ccb[0] =
  6515. cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
  6516. }
  6517. }
  6518. /*
  6519. ** Make this CCB available.
  6520. */
  6521. if (lp) {
  6522. if (cp != np->ccb)
  6523. list_move(&cp->link_ccbq, &lp->free_ccbq);
  6524. --lp->busyccbs;
  6525. if (cp->queued) {
  6526. --lp->queuedccbs;
  6527. }
  6528. }
  6529. cp -> host_status = HS_IDLE;
  6530. cp -> magic = 0;
  6531. if (cp->queued) {
  6532. --np->queuedccbs;
  6533. cp->queued = 0;
  6534. }
  6535. #if 0
  6536. if (cp == np->ccb)
  6537. wakeup ((caddr_t) cp);
  6538. #endif
  6539. }
  6540. #define ncr_reg_bus_addr(r) (np->paddr + offsetof (struct ncr_reg, r))
  6541. /*------------------------------------------------------------------------
  6542. ** Initialize the fixed part of a CCB structure.
  6543. **------------------------------------------------------------------------
  6544. **------------------------------------------------------------------------
  6545. */
  6546. static void ncr_init_ccb(struct ncb *np, struct ccb *cp)
  6547. {
  6548. ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
  6549. /*
  6550. ** Remember virtual and bus address of this ccb.
  6551. */
  6552. cp->p_ccb = vtobus(cp);
  6553. cp->phys.header.cp = cp;
  6554. /*
  6555. ** This allows list_del to work for the default ccb.
  6556. */
  6557. INIT_LIST_HEAD(&cp->link_ccbq);
  6558. /*
  6559. ** Initialyze the start and restart launch script.
  6560. **
  6561. ** COPY(4) @(...p_phys), @(dsa)
  6562. ** JUMP @(sched_point)
  6563. */
  6564. cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
  6565. cp->start.setup_dsa[1] = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
  6566. cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
  6567. cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
  6568. cp->start.p_phys = cpu_to_scr(CCB_PHYS(cp, phys));
  6569. memcpy(&cp->restart, &cp->start, sizeof(cp->restart));
  6570. cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  6571. cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
  6572. }
  6573. /*------------------------------------------------------------------------
  6574. ** Allocate a CCB and initialize its fixed part.
  6575. **------------------------------------------------------------------------
  6576. **------------------------------------------------------------------------
  6577. */
  6578. static void ncr_alloc_ccb(struct ncb *np, u_char tn, u_char ln)
  6579. {
  6580. struct tcb *tp = &np->target[tn];
  6581. struct lcb *lp = tp->lp[ln];
  6582. struct ccb *cp = NULL;
  6583. /*
  6584. ** Allocate memory for this CCB.
  6585. */
  6586. cp = m_calloc_dma(sizeof(struct ccb), "CCB");
  6587. if (!cp)
  6588. return;
  6589. /*
  6590. ** Count it and initialyze it.
  6591. */
  6592. lp->actccbs++;
  6593. np->actccbs++;
  6594. memset(cp, 0, sizeof (*cp));
  6595. ncr_init_ccb(np, cp);
  6596. /*
  6597. ** Chain into wakeup list and free ccb queue and take it
  6598. ** into account for tagged commands.
  6599. */
  6600. cp->link_ccb = np->ccb->link_ccb;
  6601. np->ccb->link_ccb = cp;
  6602. list_add(&cp->link_ccbq, &lp->free_ccbq);
  6603. }
  6604. /*==========================================================
  6605. **
  6606. **
  6607. ** Allocation of resources for Targets/Luns/Tags.
  6608. **
  6609. **
  6610. **==========================================================
  6611. */
  6612. /*------------------------------------------------------------------------
  6613. ** Target control block initialisation.
  6614. **------------------------------------------------------------------------
  6615. ** This data structure is fully initialized after a SCSI command
  6616. ** has been successfully completed for this target.
  6617. ** It contains a SCRIPT that is called on target reselection.
  6618. **------------------------------------------------------------------------
  6619. */
  6620. static void ncr_init_tcb (struct ncb *np, u_char tn)
  6621. {
  6622. struct tcb *tp = &np->target[tn];
  6623. ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
  6624. int th = tn & 3;
  6625. int i;
  6626. /*
  6627. ** Jump to next tcb if SFBR does not match this target.
  6628. ** JUMP IF (SFBR != #target#), @(next tcb)
  6629. */
  6630. tp->jump_tcb.l_cmd =
  6631. cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
  6632. tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
  6633. /*
  6634. ** Load the synchronous transfer register.
  6635. ** COPY @(tp->sval), @(sxfer)
  6636. */
  6637. tp->getscr[0] = cpu_to_scr(copy_1);
  6638. tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
  6639. #ifdef SCSI_NCR_BIG_ENDIAN
  6640. tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer) ^ 3);
  6641. #else
  6642. tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
  6643. #endif
  6644. /*
  6645. ** Load the timing register.
  6646. ** COPY @(tp->wval), @(scntl3)
  6647. */
  6648. tp->getscr[3] = cpu_to_scr(copy_1);
  6649. tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
  6650. #ifdef SCSI_NCR_BIG_ENDIAN
  6651. tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3) ^ 3);
  6652. #else
  6653. tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
  6654. #endif
  6655. /*
  6656. ** Get the IDENTIFY message and the lun.
  6657. ** CALL @script(resel_lun)
  6658. */
  6659. tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
  6660. tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
  6661. /*
  6662. ** Look for the lun control block of this nexus.
  6663. ** For i = 0 to 3
  6664. ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
  6665. */
  6666. for (i = 0 ; i < 4 ; i++) {
  6667. tp->jump_lcb[i].l_cmd =
  6668. cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
  6669. tp->jump_lcb[i].l_paddr =
  6670. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
  6671. }
  6672. /*
  6673. ** Link this target control block to the JUMP chain.
  6674. */
  6675. np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
  6676. /*
  6677. ** These assert's should be moved at driver initialisations.
  6678. */
  6679. #ifdef SCSI_NCR_BIG_ENDIAN
  6680. BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
  6681. offsetof(struct tcb , sval )) &3) != 3);
  6682. BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
  6683. offsetof(struct tcb , wval )) &3) != 3);
  6684. #else
  6685. BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
  6686. offsetof(struct tcb , sval )) &3) != 0);
  6687. BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
  6688. offsetof(struct tcb , wval )) &3) != 0);
  6689. #endif
  6690. }
  6691. /*------------------------------------------------------------------------
  6692. ** Lun control block allocation and initialization.
  6693. **------------------------------------------------------------------------
  6694. ** This data structure is allocated and initialized after a SCSI
  6695. ** command has been successfully completed for this target/lun.
  6696. **------------------------------------------------------------------------
  6697. */
  6698. static struct lcb *ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln)
  6699. {
  6700. struct tcb *tp = &np->target[tn];
  6701. struct lcb *lp = tp->lp[ln];
  6702. ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
  6703. int lh = ln & 3;
  6704. /*
  6705. ** Already done, return.
  6706. */
  6707. if (lp)
  6708. return lp;
  6709. /*
  6710. ** Allocate the lcb.
  6711. */
  6712. lp = m_calloc_dma(sizeof(struct lcb), "LCB");
  6713. if (!lp)
  6714. goto fail;
  6715. memset(lp, 0, sizeof(*lp));
  6716. tp->lp[ln] = lp;
  6717. /*
  6718. ** Initialize the target control block if not yet.
  6719. */
  6720. if (!tp->jump_tcb.l_cmd)
  6721. ncr_init_tcb(np, tn);
  6722. /*
  6723. ** Initialize the CCB queue headers.
  6724. */
  6725. INIT_LIST_HEAD(&lp->free_ccbq);
  6726. INIT_LIST_HEAD(&lp->busy_ccbq);
  6727. INIT_LIST_HEAD(&lp->wait_ccbq);
  6728. INIT_LIST_HEAD(&lp->skip_ccbq);
  6729. /*
  6730. ** Set max CCBs to 1 and use the default 1 entry
  6731. ** jump table by default.
  6732. */
  6733. lp->maxnxs = 1;
  6734. lp->jump_ccb = &lp->jump_ccb_0;
  6735. lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
  6736. /*
  6737. ** Initilialyze the reselect script:
  6738. **
  6739. ** Jump to next lcb if SFBR does not match this lun.
  6740. ** Load TEMP with the CCB direct jump table bus address.
  6741. ** Get the SIMPLE TAG message and the tag.
  6742. **
  6743. ** JUMP IF (SFBR != #lun#), @(next lcb)
  6744. ** COPY @(lp->p_jump_ccb), @(temp)
  6745. ** JUMP @script(resel_notag)
  6746. */
  6747. lp->jump_lcb.l_cmd =
  6748. cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
  6749. lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
  6750. lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
  6751. lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
  6752. lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
  6753. lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
  6754. lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
  6755. /*
  6756. ** Link this lun control block to the JUMP chain.
  6757. */
  6758. tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
  6759. /*
  6760. ** Initialize command queuing control.
  6761. */
  6762. lp->busyccbs = 1;
  6763. lp->queuedccbs = 1;
  6764. lp->queuedepth = 1;
  6765. fail:
  6766. return lp;
  6767. }
  6768. /*------------------------------------------------------------------------
  6769. ** Lun control block setup on INQUIRY data received.
  6770. **------------------------------------------------------------------------
  6771. ** We only support WIDE, SYNC for targets and CMDQ for logical units.
  6772. ** This setup is done on each INQUIRY since we are expecting user
  6773. ** will play with CHANGE DEFINITION commands. :-)
  6774. **------------------------------------------------------------------------
  6775. */
  6776. static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
  6777. {
  6778. unsigned char tn = sdev->id, ln = sdev->lun;
  6779. struct tcb *tp = &np->target[tn];
  6780. struct lcb *lp = tp->lp[ln];
  6781. /* If no lcb, try to allocate it. */
  6782. if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
  6783. goto fail;
  6784. /*
  6785. ** If unit supports tagged commands, allocate the
  6786. ** CCB JUMP table if not yet.
  6787. */
  6788. if (sdev->tagged_supported && lp->jump_ccb == &lp->jump_ccb_0) {
  6789. int i;
  6790. lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
  6791. if (!lp->jump_ccb) {
  6792. lp->jump_ccb = &lp->jump_ccb_0;
  6793. goto fail;
  6794. }
  6795. lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
  6796. for (i = 0 ; i < 64 ; i++)
  6797. lp->jump_ccb[i] =
  6798. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
  6799. for (i = 0 ; i < MAX_TAGS ; i++)
  6800. lp->cb_tags[i] = i;
  6801. lp->maxnxs = MAX_TAGS;
  6802. lp->tags_stime = jiffies + 3*HZ;
  6803. ncr_setup_tags (np, sdev);
  6804. }
  6805. fail:
  6806. return lp;
  6807. }
  6808. /*==========================================================
  6809. **
  6810. **
  6811. ** Build Scatter Gather Block
  6812. **
  6813. **
  6814. **==========================================================
  6815. **
  6816. ** The transfer area may be scattered among
  6817. ** several non adjacent physical pages.
  6818. **
  6819. ** We may use MAX_SCATTER blocks.
  6820. **
  6821. **----------------------------------------------------------
  6822. */
  6823. /*
  6824. ** We try to reduce the number of interrupts caused
  6825. ** by unexpected phase changes due to disconnects.
  6826. ** A typical harddisk may disconnect before ANY block.
  6827. ** If we wanted to avoid unexpected phase changes at all
  6828. ** we had to use a break point every 512 bytes.
  6829. ** Of course the number of scatter/gather blocks is
  6830. ** limited.
  6831. ** Under Linux, the scatter/gatter blocks are provided by
  6832. ** the generic driver. We just have to copy addresses and
  6833. ** sizes to the data segment array.
  6834. */
  6835. static int ncr_scatter_no_sglist(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
  6836. {
  6837. struct scr_tblmove *data = &cp->phys.data[MAX_SCATTER - 1];
  6838. int segment;
  6839. cp->data_len = cmd->request_bufflen;
  6840. if (cmd->request_bufflen) {
  6841. dma_addr_t baddr = map_scsi_single_data(np, cmd);
  6842. if (baddr) {
  6843. ncr_build_sge(np, data, baddr, cmd->request_bufflen);
  6844. segment = 1;
  6845. } else {
  6846. segment = -2;
  6847. }
  6848. } else {
  6849. segment = 0;
  6850. }
  6851. return segment;
  6852. }
  6853. static int ncr_scatter(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
  6854. {
  6855. int segment = 0;
  6856. int use_sg = (int) cmd->use_sg;
  6857. cp->data_len = 0;
  6858. if (!use_sg)
  6859. segment = ncr_scatter_no_sglist(np, cp, cmd);
  6860. else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
  6861. struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
  6862. struct scr_tblmove *data;
  6863. if (use_sg > MAX_SCATTER) {
  6864. unmap_scsi_data(np, cmd);
  6865. return -1;
  6866. }
  6867. data = &cp->phys.data[MAX_SCATTER - use_sg];
  6868. for (segment = 0; segment < use_sg; segment++) {
  6869. dma_addr_t baddr = sg_dma_address(&scatter[segment]);
  6870. unsigned int len = sg_dma_len(&scatter[segment]);
  6871. ncr_build_sge(np, &data[segment], baddr, len);
  6872. cp->data_len += len;
  6873. }
  6874. } else {
  6875. segment = -2;
  6876. }
  6877. return segment;
  6878. }
  6879. /*==========================================================
  6880. **
  6881. **
  6882. ** Test the bus snoop logic :-(
  6883. **
  6884. ** Has to be called with interrupts disabled.
  6885. **
  6886. **
  6887. **==========================================================
  6888. */
  6889. static int __init ncr_regtest (struct ncb* np)
  6890. {
  6891. register volatile u32 data;
  6892. /*
  6893. ** ncr registers may NOT be cached.
  6894. ** write 0xffffffff to a read only register area,
  6895. ** and try to read it back.
  6896. */
  6897. data = 0xffffffff;
  6898. OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
  6899. data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
  6900. #if 1
  6901. if (data == 0xffffffff) {
  6902. #else
  6903. if ((data & 0xe2f0fffd) != 0x02000080) {
  6904. #endif
  6905. printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  6906. (unsigned) data);
  6907. return (0x10);
  6908. }
  6909. return (0);
  6910. }
  6911. static int __init ncr_snooptest (struct ncb* np)
  6912. {
  6913. u32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
  6914. int i, err=0;
  6915. if (np->reg) {
  6916. err |= ncr_regtest (np);
  6917. if (err)
  6918. return (err);
  6919. }
  6920. /* init */
  6921. pc = NCB_SCRIPTH_PHYS (np, snooptest);
  6922. host_wr = 1;
  6923. ncr_wr = 2;
  6924. /*
  6925. ** Set memory and register.
  6926. */
  6927. np->ncr_cache = cpu_to_scr(host_wr);
  6928. OUTL (nc_temp, ncr_wr);
  6929. /*
  6930. ** Start script (exchange values)
  6931. */
  6932. OUTL_DSP (pc);
  6933. /*
  6934. ** Wait 'til done (with timeout)
  6935. */
  6936. for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
  6937. if (INB(nc_istat) & (INTF|SIP|DIP))
  6938. break;
  6939. /*
  6940. ** Save termination position.
  6941. */
  6942. pc = INL (nc_dsp);
  6943. /*
  6944. ** Read memory and register.
  6945. */
  6946. host_rd = scr_to_cpu(np->ncr_cache);
  6947. ncr_rd = INL (nc_scratcha);
  6948. ncr_bk = INL (nc_temp);
  6949. /*
  6950. ** Reset ncr chip
  6951. */
  6952. ncr_chip_reset(np, 100);
  6953. /*
  6954. ** check for timeout
  6955. */
  6956. if (i>=NCR_SNOOP_TIMEOUT) {
  6957. printk ("CACHE TEST FAILED: timeout.\n");
  6958. return (0x20);
  6959. }
  6960. /*
  6961. ** Check termination position.
  6962. */
  6963. if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
  6964. printk ("CACHE TEST FAILED: script execution failed.\n");
  6965. printk ("start=%08lx, pc=%08lx, end=%08lx\n",
  6966. (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
  6967. (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
  6968. return (0x40);
  6969. }
  6970. /*
  6971. ** Show results.
  6972. */
  6973. if (host_wr != ncr_rd) {
  6974. printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
  6975. (int) host_wr, (int) ncr_rd);
  6976. err |= 1;
  6977. }
  6978. if (host_rd != ncr_wr) {
  6979. printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
  6980. (int) ncr_wr, (int) host_rd);
  6981. err |= 2;
  6982. }
  6983. if (ncr_bk != ncr_wr) {
  6984. printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
  6985. (int) ncr_wr, (int) ncr_bk);
  6986. err |= 4;
  6987. }
  6988. return (err);
  6989. }
  6990. /*==========================================================
  6991. **
  6992. ** Determine the ncr's clock frequency.
  6993. ** This is essential for the negotiation
  6994. ** of the synchronous transfer rate.
  6995. **
  6996. **==========================================================
  6997. **
  6998. ** Note: we have to return the correct value.
  6999. ** THERE IS NO SAFE DEFAULT VALUE.
  7000. **
  7001. ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
  7002. ** 53C860 and 53C875 rev. 1 support fast20 transfers but
  7003. ** do not have a clock doubler and so are provided with a
  7004. ** 80 MHz clock. All other fast20 boards incorporate a doubler
  7005. ** and so should be delivered with a 40 MHz clock.
  7006. ** The future fast40 chips (895/895) use a 40 Mhz base clock
  7007. ** and provide a clock quadrupler (160 Mhz). The code below
  7008. ** tries to deal as cleverly as possible with all this stuff.
  7009. **
  7010. **----------------------------------------------------------
  7011. */
  7012. /*
  7013. * Select NCR SCSI clock frequency
  7014. */
  7015. static void ncr_selectclock(struct ncb *np, u_char scntl3)
  7016. {
  7017. if (np->multiplier < 2) {
  7018. OUTB(nc_scntl3, scntl3);
  7019. return;
  7020. }
  7021. if (bootverbose >= 2)
  7022. printk ("%s: enabling clock multiplier\n", ncr_name(np));
  7023. OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
  7024. if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
  7025. int i = 20;
  7026. while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
  7027. udelay(20);
  7028. if (!i)
  7029. printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
  7030. } else /* Wait 20 micro-seconds for doubler */
  7031. udelay(20);
  7032. OUTB(nc_stest3, HSC); /* Halt the scsi clock */
  7033. OUTB(nc_scntl3, scntl3);
  7034. OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
  7035. OUTB(nc_stest3, 0x00); /* Restart scsi clock */
  7036. }
  7037. /*
  7038. * calculate NCR SCSI clock frequency (in KHz)
  7039. */
  7040. static unsigned __init ncrgetfreq (struct ncb *np, int gen)
  7041. {
  7042. unsigned ms = 0;
  7043. char count = 0;
  7044. /*
  7045. * Measure GEN timer delay in order
  7046. * to calculate SCSI clock frequency
  7047. *
  7048. * This code will never execute too
  7049. * many loop iterations (if DELAY is
  7050. * reasonably correct). It could get
  7051. * too low a delay (too high a freq.)
  7052. * if the CPU is slow executing the
  7053. * loop for some reason (an NMI, for
  7054. * example). For this reason we will
  7055. * if multiple measurements are to be
  7056. * performed trust the higher delay
  7057. * (lower frequency returned).
  7058. */
  7059. OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
  7060. OUTW (nc_sien , 0); /* mask all scsi interrupts */
  7061. (void) INW (nc_sist); /* clear pending scsi interrupt */
  7062. OUTB (nc_dien , 0); /* mask all dma interrupts */
  7063. (void) INW (nc_sist); /* another one, just to be sure :) */
  7064. OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
  7065. OUTB (nc_stime1, 0); /* disable general purpose timer */
  7066. OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
  7067. while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
  7068. for (count = 0; count < 10; count ++)
  7069. udelay(100); /* count ms */
  7070. }
  7071. OUTB (nc_stime1, 0); /* disable general purpose timer */
  7072. /*
  7073. * set prescaler to divide by whatever 0 means
  7074. * 0 ought to choose divide by 2, but appears
  7075. * to set divide by 3.5 mode in my 53c810 ...
  7076. */
  7077. OUTB (nc_scntl3, 0);
  7078. if (bootverbose >= 2)
  7079. printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
  7080. /*
  7081. * adjust for prescaler, and convert into KHz
  7082. */
  7083. return ms ? ((1 << gen) * 4340) / ms : 0;
  7084. }
  7085. /*
  7086. * Get/probe NCR SCSI clock frequency
  7087. */
  7088. static void __init ncr_getclock (struct ncb *np, int mult)
  7089. {
  7090. unsigned char scntl3 = INB(nc_scntl3);
  7091. unsigned char stest1 = INB(nc_stest1);
  7092. unsigned f1;
  7093. np->multiplier = 1;
  7094. f1 = 40000;
  7095. /*
  7096. ** True with 875 or 895 with clock multiplier selected
  7097. */
  7098. if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
  7099. if (bootverbose >= 2)
  7100. printk ("%s: clock multiplier found\n", ncr_name(np));
  7101. np->multiplier = mult;
  7102. }
  7103. /*
  7104. ** If multiplier not found or scntl3 not 7,5,3,
  7105. ** reset chip and get frequency from general purpose timer.
  7106. ** Otherwise trust scntl3 BIOS setting.
  7107. */
  7108. if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
  7109. unsigned f2;
  7110. ncr_chip_reset(np, 5);
  7111. (void) ncrgetfreq (np, 11); /* throw away first result */
  7112. f1 = ncrgetfreq (np, 11);
  7113. f2 = ncrgetfreq (np, 11);
  7114. if(bootverbose)
  7115. printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
  7116. if (f1 > f2) f1 = f2; /* trust lower result */
  7117. if (f1 < 45000) f1 = 40000;
  7118. else if (f1 < 55000) f1 = 50000;
  7119. else f1 = 80000;
  7120. if (f1 < 80000 && mult > 1) {
  7121. if (bootverbose >= 2)
  7122. printk ("%s: clock multiplier assumed\n", ncr_name(np));
  7123. np->multiplier = mult;
  7124. }
  7125. } else {
  7126. if ((scntl3 & 7) == 3) f1 = 40000;
  7127. else if ((scntl3 & 7) == 5) f1 = 80000;
  7128. else f1 = 160000;
  7129. f1 /= np->multiplier;
  7130. }
  7131. /*
  7132. ** Compute controller synchronous parameters.
  7133. */
  7134. f1 *= np->multiplier;
  7135. np->clock_khz = f1;
  7136. }
  7137. /*===================== LINUX ENTRY POINTS SECTION ==========================*/
  7138. static int ncr53c8xx_slave_alloc(struct scsi_device *device)
  7139. {
  7140. struct Scsi_Host *host = device->host;
  7141. struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
  7142. struct tcb *tp = &np->target[device->id];
  7143. tp->starget = device->sdev_target;
  7144. return 0;
  7145. }
  7146. static int ncr53c8xx_slave_configure(struct scsi_device *device)
  7147. {
  7148. struct Scsi_Host *host = device->host;
  7149. struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
  7150. struct tcb *tp = &np->target[device->id];
  7151. struct lcb *lp = tp->lp[device->lun];
  7152. int numtags, depth_to_use;
  7153. ncr_setup_lcb(np, device);
  7154. /*
  7155. ** Select queue depth from driver setup.
  7156. ** Donnot use more than configured by user.
  7157. ** Use at least 2.
  7158. ** Donnot use more than our maximum.
  7159. */
  7160. numtags = device_queue_depth(np->unit, device->id, device->lun);
  7161. if (numtags > tp->usrtags)
  7162. numtags = tp->usrtags;
  7163. if (!device->tagged_supported)
  7164. numtags = 1;
  7165. depth_to_use = numtags;
  7166. if (depth_to_use < 2)
  7167. depth_to_use = 2;
  7168. if (depth_to_use > MAX_TAGS)
  7169. depth_to_use = MAX_TAGS;
  7170. scsi_adjust_queue_depth(device,
  7171. (device->tagged_supported ?
  7172. MSG_SIMPLE_TAG : 0),
  7173. depth_to_use);
  7174. /*
  7175. ** Since the queue depth is not tunable under Linux,
  7176. ** we need to know this value in order not to
  7177. ** announce stupid things to user.
  7178. **
  7179. ** XXX(hch): As of Linux 2.6 it certainly _is_ tunable..
  7180. ** In fact we just tuned it, or did I miss
  7181. ** something important? :)
  7182. */
  7183. if (lp) {
  7184. lp->numtags = lp->maxtags = numtags;
  7185. lp->scdev_depth = depth_to_use;
  7186. }
  7187. ncr_setup_tags (np, device);
  7188. #ifdef DEBUG_NCR53C8XX
  7189. printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
  7190. np->unit, device->id, device->lun, depth_to_use);
  7191. #endif
  7192. if (spi_support_sync(device->sdev_target) &&
  7193. !spi_initial_dv(device->sdev_target))
  7194. spi_dv_device(device);
  7195. return 0;
  7196. }
  7197. static int ncr53c8xx_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
  7198. {
  7199. struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
  7200. unsigned long flags;
  7201. int sts;
  7202. #ifdef DEBUG_NCR53C8XX
  7203. printk("ncr53c8xx_queue_command\n");
  7204. #endif
  7205. cmd->scsi_done = done;
  7206. cmd->host_scribble = NULL;
  7207. cmd->__data_mapped = 0;
  7208. cmd->__data_mapping = 0;
  7209. spin_lock_irqsave(&np->smp_lock, flags);
  7210. if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
  7211. cmd->result = ScsiResult(sts, 0);
  7212. #ifdef DEBUG_NCR53C8XX
  7213. printk("ncr53c8xx : command not queued - result=%d\n", sts);
  7214. #endif
  7215. }
  7216. #ifdef DEBUG_NCR53C8XX
  7217. else
  7218. printk("ncr53c8xx : command successfully queued\n");
  7219. #endif
  7220. spin_unlock_irqrestore(&np->smp_lock, flags);
  7221. if (sts != DID_OK) {
  7222. unmap_scsi_data(np, cmd);
  7223. done(cmd);
  7224. sts = 0;
  7225. }
  7226. return sts;
  7227. }
  7228. irqreturn_t ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
  7229. {
  7230. unsigned long flags;
  7231. struct Scsi_Host *shost = (struct Scsi_Host *)dev_id;
  7232. struct host_data *host_data = (struct host_data *)shost->hostdata;
  7233. struct ncb *np = host_data->ncb;
  7234. struct scsi_cmnd *done_list;
  7235. #ifdef DEBUG_NCR53C8XX
  7236. printk("ncr53c8xx : interrupt received\n");
  7237. #endif
  7238. if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
  7239. spin_lock_irqsave(&np->smp_lock, flags);
  7240. ncr_exception(np);
  7241. done_list = np->done_list;
  7242. np->done_list = NULL;
  7243. spin_unlock_irqrestore(&np->smp_lock, flags);
  7244. if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
  7245. if (done_list)
  7246. ncr_flush_done_cmds(done_list);
  7247. return IRQ_HANDLED;
  7248. }
  7249. static void ncr53c8xx_timeout(unsigned long npref)
  7250. {
  7251. struct ncb *np = (struct ncb *) npref;
  7252. unsigned long flags;
  7253. struct scsi_cmnd *done_list;
  7254. spin_lock_irqsave(&np->smp_lock, flags);
  7255. ncr_timeout(np);
  7256. done_list = np->done_list;
  7257. np->done_list = NULL;
  7258. spin_unlock_irqrestore(&np->smp_lock, flags);
  7259. if (done_list)
  7260. ncr_flush_done_cmds(done_list);
  7261. }
  7262. static int ncr53c8xx_bus_reset(struct scsi_cmnd *cmd)
  7263. {
  7264. struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
  7265. int sts;
  7266. unsigned long flags;
  7267. struct scsi_cmnd *done_list;
  7268. /*
  7269. * If the mid-level driver told us reset is synchronous, it seems
  7270. * that we must call the done() callback for the involved command,
  7271. * even if this command was not queued to the low-level driver,
  7272. * before returning SUCCESS.
  7273. */
  7274. spin_lock_irqsave(&np->smp_lock, flags);
  7275. sts = ncr_reset_bus(np, cmd, 1);
  7276. done_list = np->done_list;
  7277. np->done_list = NULL;
  7278. spin_unlock_irqrestore(&np->smp_lock, flags);
  7279. ncr_flush_done_cmds(done_list);
  7280. return sts;
  7281. }
  7282. #if 0 /* unused and broken */
  7283. static int ncr53c8xx_abort(struct scsi_cmnd *cmd)
  7284. {
  7285. struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
  7286. int sts;
  7287. unsigned long flags;
  7288. struct scsi_cmnd *done_list;
  7289. #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
  7290. printk("ncr53c8xx_abort: pid=%lu serial_number=%ld\n",
  7291. cmd->pid, cmd->serial_number);
  7292. #else
  7293. printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
  7294. #endif
  7295. NCR_LOCK_NCB(np, flags);
  7296. sts = ncr_abort_command(np, cmd);
  7297. out:
  7298. done_list = np->done_list;
  7299. np->done_list = NULL;
  7300. NCR_UNLOCK_NCB(np, flags);
  7301. ncr_flush_done_cmds(done_list);
  7302. return sts;
  7303. }
  7304. #endif
  7305. /*
  7306. ** Scsi command waiting list management.
  7307. **
  7308. ** It may happen that we cannot insert a scsi command into the start queue,
  7309. ** in the following circumstances.
  7310. ** Too few preallocated ccb(s),
  7311. ** maxtags < cmd_per_lun of the Linux host control block,
  7312. ** etc...
  7313. ** Such scsi commands are inserted into a waiting list.
  7314. ** When a scsi command complete, we try to requeue the commands of the
  7315. ** waiting list.
  7316. */
  7317. #define next_wcmd host_scribble
  7318. static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd)
  7319. {
  7320. struct scsi_cmnd *wcmd;
  7321. #ifdef DEBUG_WAITING_LIST
  7322. printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
  7323. #endif
  7324. cmd->next_wcmd = NULL;
  7325. if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
  7326. else {
  7327. while ((wcmd->next_wcmd) != 0)
  7328. wcmd = (struct scsi_cmnd *) wcmd->next_wcmd;
  7329. wcmd->next_wcmd = (char *) cmd;
  7330. }
  7331. }
  7332. static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd)
  7333. {
  7334. struct scsi_cmnd **pcmd = &np->waiting_list;
  7335. while (*pcmd) {
  7336. if (cmd == *pcmd) {
  7337. if (to_remove) {
  7338. *pcmd = (struct scsi_cmnd *) cmd->next_wcmd;
  7339. cmd->next_wcmd = NULL;
  7340. }
  7341. #ifdef DEBUG_WAITING_LIST
  7342. printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
  7343. #endif
  7344. return cmd;
  7345. }
  7346. pcmd = (struct scsi_cmnd **) &(*pcmd)->next_wcmd;
  7347. }
  7348. return NULL;
  7349. }
  7350. static void process_waiting_list(struct ncb *np, int sts)
  7351. {
  7352. struct scsi_cmnd *waiting_list, *wcmd;
  7353. waiting_list = np->waiting_list;
  7354. np->waiting_list = NULL;
  7355. #ifdef DEBUG_WAITING_LIST
  7356. if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
  7357. #endif
  7358. while ((wcmd = waiting_list) != 0) {
  7359. waiting_list = (struct scsi_cmnd *) wcmd->next_wcmd;
  7360. wcmd->next_wcmd = NULL;
  7361. if (sts == DID_OK) {
  7362. #ifdef DEBUG_WAITING_LIST
  7363. printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
  7364. #endif
  7365. sts = ncr_queue_command(np, wcmd);
  7366. }
  7367. if (sts != DID_OK) {
  7368. #ifdef DEBUG_WAITING_LIST
  7369. printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
  7370. #endif
  7371. wcmd->result = ScsiResult(sts, 0);
  7372. ncr_queue_done_cmd(np, wcmd);
  7373. }
  7374. }
  7375. }
  7376. #undef next_wcmd
  7377. static ssize_t show_ncr53c8xx_revision(struct class_device *dev, char *buf)
  7378. {
  7379. struct Scsi_Host *host = class_to_shost(dev);
  7380. struct host_data *host_data = (struct host_data *)host->hostdata;
  7381. return snprintf(buf, 20, "0x%x\n", host_data->ncb->revision_id);
  7382. }
  7383. static struct class_device_attribute ncr53c8xx_revision_attr = {
  7384. .attr = { .name = "revision", .mode = S_IRUGO, },
  7385. .show = show_ncr53c8xx_revision,
  7386. };
  7387. static struct class_device_attribute *ncr53c8xx_host_attrs[] = {
  7388. &ncr53c8xx_revision_attr,
  7389. NULL
  7390. };
  7391. /*==========================================================
  7392. **
  7393. ** Boot command line.
  7394. **
  7395. **==========================================================
  7396. */
  7397. #ifdef MODULE
  7398. char *ncr53c8xx; /* command line passed by insmod */
  7399. module_param(ncr53c8xx, charp, 0);
  7400. #endif
  7401. static int __init ncr53c8xx_setup(char *str)
  7402. {
  7403. return sym53c8xx__setup(str);
  7404. }
  7405. #ifndef MODULE
  7406. __setup("ncr53c8xx=", ncr53c8xx_setup);
  7407. #endif
  7408. /*
  7409. * Host attach and initialisations.
  7410. *
  7411. * Allocate host data and ncb structure.
  7412. * Request IO region and remap MMIO region.
  7413. * Do chip initialization.
  7414. * If all is OK, install interrupt handling and
  7415. * start the timer daemon.
  7416. */
  7417. struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
  7418. int unit, struct ncr_device *device)
  7419. {
  7420. struct host_data *host_data;
  7421. struct ncb *np = NULL;
  7422. struct Scsi_Host *instance = NULL;
  7423. u_long flags = 0;
  7424. int i;
  7425. if (!tpnt->name)
  7426. tpnt->name = SCSI_NCR_DRIVER_NAME;
  7427. if (!tpnt->shost_attrs)
  7428. tpnt->shost_attrs = ncr53c8xx_host_attrs;
  7429. tpnt->queuecommand = ncr53c8xx_queue_command;
  7430. tpnt->slave_configure = ncr53c8xx_slave_configure;
  7431. tpnt->slave_alloc = ncr53c8xx_slave_alloc;
  7432. tpnt->eh_bus_reset_handler = ncr53c8xx_bus_reset;
  7433. tpnt->can_queue = SCSI_NCR_CAN_QUEUE;
  7434. tpnt->this_id = 7;
  7435. tpnt->sg_tablesize = SCSI_NCR_SG_TABLESIZE;
  7436. tpnt->cmd_per_lun = SCSI_NCR_CMD_PER_LUN;
  7437. tpnt->use_clustering = ENABLE_CLUSTERING;
  7438. if (device->differential)
  7439. driver_setup.diff_support = device->differential;
  7440. printk(KERN_INFO "ncr53c720-%d: rev 0x%x irq %d\n",
  7441. unit, device->chip.revision_id, device->slot.irq);
  7442. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  7443. if (!instance)
  7444. goto attach_error;
  7445. host_data = (struct host_data *) instance->hostdata;
  7446. np = __m_calloc_dma(device->dev, sizeof(struct ncb), "NCB");
  7447. if (!np)
  7448. goto attach_error;
  7449. spin_lock_init(&np->smp_lock);
  7450. np->dev = device->dev;
  7451. np->p_ncb = vtobus(np);
  7452. host_data->ncb = np;
  7453. np->ccb = m_calloc_dma(sizeof(struct ccb), "CCB");
  7454. if (!np->ccb)
  7455. goto attach_error;
  7456. /* Store input information in the host data structure. */
  7457. np->unit = unit;
  7458. np->verbose = driver_setup.verbose;
  7459. sprintf(np->inst_name, "ncr53c720-%d", np->unit);
  7460. np->revision_id = device->chip.revision_id;
  7461. np->features = device->chip.features;
  7462. np->clock_divn = device->chip.nr_divisor;
  7463. np->maxoffs = device->chip.offset_max;
  7464. np->maxburst = device->chip.burst_max;
  7465. np->myaddr = device->host_id;
  7466. /* Allocate SCRIPTS areas. */
  7467. np->script0 = m_calloc_dma(sizeof(struct script), "SCRIPT");
  7468. if (!np->script0)
  7469. goto attach_error;
  7470. np->scripth0 = m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
  7471. if (!np->scripth0)
  7472. goto attach_error;
  7473. init_timer(&np->timer);
  7474. np->timer.data = (unsigned long) np;
  7475. np->timer.function = ncr53c8xx_timeout;
  7476. /* Try to map the controller chip to virtual and physical memory. */
  7477. np->paddr = device->slot.base;
  7478. np->paddr2 = (np->features & FE_RAM) ? device->slot.base_2 : 0;
  7479. if (device->slot.base_v)
  7480. np->vaddr = device->slot.base_v;
  7481. else
  7482. np->vaddr = ioremap(device->slot.base_c, 128);
  7483. if (!np->vaddr) {
  7484. printk(KERN_ERR
  7485. "%s: can't map memory mapped IO region\n",ncr_name(np));
  7486. goto attach_error;
  7487. } else {
  7488. if (bootverbose > 1)
  7489. printk(KERN_INFO
  7490. "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
  7491. }
  7492. /* Make the controller's registers available. Now the INB INW INL
  7493. * OUTB OUTW OUTL macros can be used safely.
  7494. */
  7495. np->reg = (struct ncr_reg __iomem *)np->vaddr;
  7496. /* Do chip dependent initialization. */
  7497. ncr_prepare_setting(np);
  7498. if (np->paddr2 && sizeof(struct script) > 4096) {
  7499. np->paddr2 = 0;
  7500. printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
  7501. ncr_name(np));
  7502. }
  7503. instance->max_channel = 0;
  7504. instance->this_id = np->myaddr;
  7505. instance->max_id = np->maxwide ? 16 : 8;
  7506. instance->max_lun = SCSI_NCR_MAX_LUN;
  7507. instance->base = (unsigned long) np->reg;
  7508. instance->irq = device->slot.irq;
  7509. instance->unique_id = device->slot.base;
  7510. instance->dma_channel = 0;
  7511. instance->cmd_per_lun = MAX_TAGS;
  7512. instance->can_queue = (MAX_START-4);
  7513. /* This can happen if you forget to call ncr53c8xx_init from
  7514. * your module_init */
  7515. BUG_ON(!ncr53c8xx_transport_template);
  7516. instance->transportt = ncr53c8xx_transport_template;
  7517. /* Patch script to physical addresses */
  7518. ncr_script_fill(&script0, &scripth0);
  7519. np->scripth = np->scripth0;
  7520. np->p_scripth = vtobus(np->scripth);
  7521. np->p_script = (np->paddr2) ? np->paddr2 : vtobus(np->script0);
  7522. ncr_script_copy_and_bind(np, (ncrcmd *) &script0,
  7523. (ncrcmd *) np->script0, sizeof(struct script));
  7524. ncr_script_copy_and_bind(np, (ncrcmd *) &scripth0,
  7525. (ncrcmd *) np->scripth0, sizeof(struct scripth));
  7526. np->ccb->p_ccb = vtobus (np->ccb);
  7527. /* Patch the script for LED support. */
  7528. if (np->features & FE_LED0) {
  7529. np->script0->idle[0] =
  7530. cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
  7531. np->script0->reselected[0] =
  7532. cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
  7533. np->script0->start[0] =
  7534. cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
  7535. }
  7536. /*
  7537. * Look for the target control block of this nexus.
  7538. * For i = 0 to 3
  7539. * JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
  7540. */
  7541. for (i = 0 ; i < 4 ; i++) {
  7542. np->jump_tcb[i].l_cmd =
  7543. cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
  7544. np->jump_tcb[i].l_paddr =
  7545. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
  7546. }
  7547. ncr_chip_reset(np, 100);
  7548. /* Now check the cache handling of the chipset. */
  7549. if (ncr_snooptest(np)) {
  7550. printk(KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
  7551. goto attach_error;
  7552. }
  7553. /* Install the interrupt handler. */
  7554. np->irq = device->slot.irq;
  7555. /* Initialize the fixed part of the default ccb. */
  7556. ncr_init_ccb(np, np->ccb);
  7557. /*
  7558. * After SCSI devices have been opened, we cannot reset the bus
  7559. * safely, so we do it here. Interrupt handler does the real work.
  7560. * Process the reset exception if interrupts are not enabled yet.
  7561. * Then enable disconnects.
  7562. */
  7563. spin_lock_irqsave(&np->smp_lock, flags);
  7564. if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
  7565. printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
  7566. spin_unlock_irqrestore(&np->smp_lock, flags);
  7567. goto attach_error;
  7568. }
  7569. ncr_exception(np);
  7570. np->disc = 1;
  7571. /*
  7572. * The middle-level SCSI driver does not wait for devices to settle.
  7573. * Wait synchronously if more than 2 seconds.
  7574. */
  7575. if (driver_setup.settle_delay > 2) {
  7576. printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
  7577. ncr_name(np), driver_setup.settle_delay);
  7578. mdelay(1000 * driver_setup.settle_delay);
  7579. }
  7580. /* start the timeout daemon */
  7581. np->lasttime=0;
  7582. ncr_timeout (np);
  7583. /* use SIMPLE TAG messages by default */
  7584. #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
  7585. np->order = SIMPLE_QUEUE_TAG;
  7586. #endif
  7587. spin_unlock_irqrestore(&np->smp_lock, flags);
  7588. return instance;
  7589. attach_error:
  7590. if (!instance)
  7591. return NULL;
  7592. printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
  7593. if (!np)
  7594. goto unregister;
  7595. if (np->scripth0)
  7596. m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
  7597. if (np->script0)
  7598. m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
  7599. if (np->ccb)
  7600. m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
  7601. m_free_dma(np, sizeof(struct ncb), "NCB");
  7602. host_data->ncb = NULL;
  7603. unregister:
  7604. scsi_host_put(instance);
  7605. return NULL;
  7606. }
  7607. int ncr53c8xx_release(struct Scsi_Host *host)
  7608. {
  7609. struct host_data *host_data;
  7610. #ifdef DEBUG_NCR53C8XX
  7611. printk("ncr53c8xx: release\n");
  7612. #endif
  7613. if (!host)
  7614. return 1;
  7615. host_data = (struct host_data *)host->hostdata;
  7616. if (host_data && host_data->ncb)
  7617. ncr_detach(host_data->ncb);
  7618. return 1;
  7619. }
  7620. static void ncr53c8xx_set_period(struct scsi_target *starget, int period)
  7621. {
  7622. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7623. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7624. struct tcb *tp = &np->target[starget->id];
  7625. if (period > np->maxsync)
  7626. period = np->maxsync;
  7627. else if (period < np->minsync)
  7628. period = np->minsync;
  7629. tp->usrsync = period;
  7630. ncr_negotiate(np, tp);
  7631. }
  7632. static void ncr53c8xx_set_offset(struct scsi_target *starget, int offset)
  7633. {
  7634. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7635. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7636. struct tcb *tp = &np->target[starget->id];
  7637. if (offset > np->maxoffs)
  7638. offset = np->maxoffs;
  7639. else if (offset < 0)
  7640. offset = 0;
  7641. tp->maxoffs = offset;
  7642. ncr_negotiate(np, tp);
  7643. }
  7644. static void ncr53c8xx_set_width(struct scsi_target *starget, int width)
  7645. {
  7646. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7647. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7648. struct tcb *tp = &np->target[starget->id];
  7649. if (width > np->maxwide)
  7650. width = np->maxwide;
  7651. else if (width < 0)
  7652. width = 0;
  7653. tp->usrwide = width;
  7654. ncr_negotiate(np, tp);
  7655. }
  7656. static void ncr53c8xx_get_signalling(struct Scsi_Host *shost)
  7657. {
  7658. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7659. enum spi_signal_type type;
  7660. switch (np->scsi_mode) {
  7661. case SMODE_SE:
  7662. type = SPI_SIGNAL_SE;
  7663. break;
  7664. case SMODE_HVD:
  7665. type = SPI_SIGNAL_HVD;
  7666. break;
  7667. default:
  7668. type = SPI_SIGNAL_UNKNOWN;
  7669. break;
  7670. }
  7671. spi_signalling(shost) = type;
  7672. }
  7673. static struct spi_function_template ncr53c8xx_transport_functions = {
  7674. .set_period = ncr53c8xx_set_period,
  7675. .show_period = 1,
  7676. .set_offset = ncr53c8xx_set_offset,
  7677. .show_offset = 1,
  7678. .set_width = ncr53c8xx_set_width,
  7679. .show_width = 1,
  7680. .get_signalling = ncr53c8xx_get_signalling,
  7681. };
  7682. int __init ncr53c8xx_init(void)
  7683. {
  7684. ncr53c8xx_transport_template = spi_attach_transport(&ncr53c8xx_transport_functions);
  7685. if (!ncr53c8xx_transport_template)
  7686. return -ENODEV;
  7687. return 0;
  7688. }
  7689. void ncr53c8xx_exit(void)
  7690. {
  7691. spi_release_transport(ncr53c8xx_transport_template);
  7692. }