ncr53c8xx.c 212 KB

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