ncr53c8xx.c 211 KB

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