ncr53c8xx.c 211 KB

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