ncr53c8xx.c 212 KB

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