aic79xx_core.c 266 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788
  1. /*
  2. * Core routines and tables shareable across OS platforms.
  3. *
  4. * Copyright (c) 1994-2002 Justin T. Gibbs.
  5. * Copyright (c) 2000-2003 Adaptec Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions, and the following disclaimer,
  13. * without modification.
  14. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  15. * substantially similar to the "NO WARRANTY" disclaimer below
  16. * ("Disclaimer") and any redistribution must be conditioned upon
  17. * including a substantially similar Disclaimer requirement for further
  18. * binary redistribution.
  19. * 3. Neither the names of the above-listed copyright holders nor the names
  20. * of any contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * Alternatively, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2 as published by the Free
  25. * Software Foundation.
  26. *
  27. * NO WARRANTY
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGES.
  39. *
  40. * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#202 $
  41. *
  42. * $FreeBSD$
  43. */
  44. #ifdef __linux__
  45. #include "aic79xx_osm.h"
  46. #include "aic79xx_inline.h"
  47. #include "aicasm/aicasm_insformat.h"
  48. #else
  49. #include <dev/aic7xxx/aic79xx_osm.h>
  50. #include <dev/aic7xxx/aic79xx_inline.h>
  51. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  52. #endif
  53. /***************************** Lookup Tables **********************************/
  54. char *ahd_chip_names[] =
  55. {
  56. "NONE",
  57. "aic7901",
  58. "aic7902",
  59. "aic7901A"
  60. };
  61. static const u_int num_chip_names = NUM_ELEMENTS(ahd_chip_names);
  62. /*
  63. * Hardware error codes.
  64. */
  65. struct ahd_hard_error_entry {
  66. uint8_t errno;
  67. char *errmesg;
  68. };
  69. static struct ahd_hard_error_entry ahd_hard_errors[] = {
  70. { DSCTMOUT, "Discard Timer has timed out" },
  71. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  72. { SQPARERR, "Sequencer Parity Error" },
  73. { DPARERR, "Data-path Parity Error" },
  74. { MPARERR, "Scratch or SCB Memory Parity Error" },
  75. { CIOPARERR, "CIOBUS Parity Error" },
  76. };
  77. static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
  78. static struct ahd_phase_table_entry ahd_phase_table[] =
  79. {
  80. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  81. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  82. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  83. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  84. { P_COMMAND, MSG_NOOP, "in Command phase" },
  85. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  86. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  87. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  88. { P_BUSFREE, MSG_NOOP, "while idle" },
  89. { 0, MSG_NOOP, "in unknown phase" }
  90. };
  91. /*
  92. * In most cases we only wish to itterate over real phases, so
  93. * exclude the last element from the count.
  94. */
  95. static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
  96. /* Our Sequencer Program */
  97. #include "aic79xx_seq.h"
  98. /**************************** Function Declarations ***************************/
  99. static void ahd_handle_transmission_error(struct ahd_softc *ahd);
  100. static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
  101. u_int lqistat1);
  102. static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
  103. u_int busfreetime);
  104. static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
  105. static void ahd_handle_proto_violation(struct ahd_softc *ahd);
  106. static void ahd_force_renegotiation(struct ahd_softc *ahd,
  107. struct ahd_devinfo *devinfo);
  108. static struct ahd_tmode_tstate*
  109. ahd_alloc_tstate(struct ahd_softc *ahd,
  110. u_int scsi_id, char channel);
  111. #ifdef AHD_TARGET_MODE
  112. static void ahd_free_tstate(struct ahd_softc *ahd,
  113. u_int scsi_id, char channel, int force);
  114. #endif
  115. static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
  116. struct ahd_initiator_tinfo *,
  117. u_int *period,
  118. u_int *ppr_options,
  119. role_t role);
  120. static void ahd_update_neg_table(struct ahd_softc *ahd,
  121. struct ahd_devinfo *devinfo,
  122. struct ahd_transinfo *tinfo);
  123. static void ahd_update_pending_scbs(struct ahd_softc *ahd);
  124. static void ahd_fetch_devinfo(struct ahd_softc *ahd,
  125. struct ahd_devinfo *devinfo);
  126. static void ahd_scb_devinfo(struct ahd_softc *ahd,
  127. struct ahd_devinfo *devinfo,
  128. struct scb *scb);
  129. static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
  130. struct ahd_devinfo *devinfo,
  131. struct scb *scb);
  132. static void ahd_build_transfer_msg(struct ahd_softc *ahd,
  133. struct ahd_devinfo *devinfo);
  134. static void ahd_construct_sdtr(struct ahd_softc *ahd,
  135. struct ahd_devinfo *devinfo,
  136. u_int period, u_int offset);
  137. static void ahd_construct_wdtr(struct ahd_softc *ahd,
  138. struct ahd_devinfo *devinfo,
  139. u_int bus_width);
  140. static void ahd_construct_ppr(struct ahd_softc *ahd,
  141. struct ahd_devinfo *devinfo,
  142. u_int period, u_int offset,
  143. u_int bus_width, u_int ppr_options);
  144. static void ahd_clear_msg_state(struct ahd_softc *ahd);
  145. static void ahd_handle_message_phase(struct ahd_softc *ahd);
  146. typedef enum {
  147. AHDMSG_1B,
  148. AHDMSG_2B,
  149. AHDMSG_EXT
  150. } ahd_msgtype;
  151. static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
  152. u_int msgval, int full);
  153. static int ahd_parse_msg(struct ahd_softc *ahd,
  154. struct ahd_devinfo *devinfo);
  155. static int ahd_handle_msg_reject(struct ahd_softc *ahd,
  156. struct ahd_devinfo *devinfo);
  157. static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
  158. struct ahd_devinfo *devinfo);
  159. static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
  160. static void ahd_handle_devreset(struct ahd_softc *ahd,
  161. struct ahd_devinfo *devinfo,
  162. u_int lun, cam_status status,
  163. char *message, int verbose_level);
  164. #ifdef AHD_TARGET_MODE
  165. static void ahd_setup_target_msgin(struct ahd_softc *ahd,
  166. struct ahd_devinfo *devinfo,
  167. struct scb *scb);
  168. #endif
  169. static u_int ahd_sglist_size(struct ahd_softc *ahd);
  170. static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
  171. static bus_dmamap_callback_t
  172. ahd_dmamap_cb;
  173. static void ahd_initialize_hscbs(struct ahd_softc *ahd);
  174. static int ahd_init_scbdata(struct ahd_softc *ahd);
  175. static void ahd_fini_scbdata(struct ahd_softc *ahd);
  176. static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
  177. static void ahd_iocell_first_selection(struct ahd_softc *ahd);
  178. static void ahd_add_col_list(struct ahd_softc *ahd,
  179. struct scb *scb, u_int col_idx);
  180. static void ahd_rem_col_list(struct ahd_softc *ahd,
  181. struct scb *scb);
  182. static void ahd_chip_init(struct ahd_softc *ahd);
  183. static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
  184. struct scb *prev_scb,
  185. struct scb *scb);
  186. static int ahd_qinfifo_count(struct ahd_softc *ahd);
  187. static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
  188. char channel, int lun, u_int tag,
  189. role_t role, uint32_t status,
  190. ahd_search_action action,
  191. u_int *list_head, u_int tid);
  192. static void ahd_stitch_tid_list(struct ahd_softc *ahd,
  193. u_int tid_prev, u_int tid_cur,
  194. u_int tid_next);
  195. static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
  196. u_int scbid);
  197. static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  198. u_int prev, u_int next, u_int tid);
  199. static void ahd_reset_current_bus(struct ahd_softc *ahd);
  200. static ahd_callback_t ahd_reset_poll;
  201. static ahd_callback_t ahd_stat_timer;
  202. #ifdef AHD_DUMP_SEQ
  203. static void ahd_dumpseq(struct ahd_softc *ahd);
  204. #endif
  205. static void ahd_loadseq(struct ahd_softc *ahd);
  206. static int ahd_check_patch(struct ahd_softc *ahd,
  207. struct patch **start_patch,
  208. u_int start_instr, u_int *skip_addr);
  209. static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
  210. u_int address);
  211. static void ahd_download_instr(struct ahd_softc *ahd,
  212. u_int instrptr, uint8_t *dconsts);
  213. static int ahd_probe_stack_size(struct ahd_softc *ahd);
  214. static int ahd_scb_active_in_fifo(struct ahd_softc *ahd,
  215. struct scb *scb);
  216. static void ahd_run_data_fifo(struct ahd_softc *ahd,
  217. struct scb *scb);
  218. #ifdef AHD_TARGET_MODE
  219. static void ahd_queue_lstate_event(struct ahd_softc *ahd,
  220. struct ahd_tmode_lstate *lstate,
  221. u_int initiator_id,
  222. u_int event_type,
  223. u_int event_arg);
  224. static void ahd_update_scsiid(struct ahd_softc *ahd,
  225. u_int targid_mask);
  226. static int ahd_handle_target_cmd(struct ahd_softc *ahd,
  227. struct target_cmd *cmd);
  228. #endif
  229. /******************************** Private Inlines *****************************/
  230. static __inline void ahd_assert_atn(struct ahd_softc *ahd);
  231. static __inline int ahd_currently_packetized(struct ahd_softc *ahd);
  232. static __inline int ahd_set_active_fifo(struct ahd_softc *ahd);
  233. static __inline void
  234. ahd_assert_atn(struct ahd_softc *ahd)
  235. {
  236. ahd_outb(ahd, SCSISIGO, ATNO);
  237. }
  238. /*
  239. * Determine if the current connection has a packetized
  240. * agreement. This does not necessarily mean that we
  241. * are currently in a packetized transfer. We could
  242. * just as easily be sending or receiving a message.
  243. */
  244. static __inline int
  245. ahd_currently_packetized(struct ahd_softc *ahd)
  246. {
  247. ahd_mode_state saved_modes;
  248. int packetized;
  249. saved_modes = ahd_save_modes(ahd);
  250. if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
  251. /*
  252. * The packetized bit refers to the last
  253. * connection, not the current one. Check
  254. * for non-zero LQISTATE instead.
  255. */
  256. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  257. packetized = ahd_inb(ahd, LQISTATE) != 0;
  258. } else {
  259. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  260. packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
  261. }
  262. ahd_restore_modes(ahd, saved_modes);
  263. return (packetized);
  264. }
  265. static __inline int
  266. ahd_set_active_fifo(struct ahd_softc *ahd)
  267. {
  268. u_int active_fifo;
  269. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  270. active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  271. switch (active_fifo) {
  272. case 0:
  273. case 1:
  274. ahd_set_modes(ahd, active_fifo, active_fifo);
  275. return (1);
  276. default:
  277. return (0);
  278. }
  279. }
  280. /************************* Sequencer Execution Control ************************/
  281. /*
  282. * Restart the sequencer program from address zero
  283. */
  284. void
  285. ahd_restart(struct ahd_softc *ahd)
  286. {
  287. ahd_pause(ahd);
  288. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  289. /* No more pending messages */
  290. ahd_clear_msg_state(ahd);
  291. ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
  292. ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
  293. ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
  294. ahd_outb(ahd, SEQINTCTL, 0);
  295. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  296. ahd_outb(ahd, SEQ_FLAGS, 0);
  297. ahd_outb(ahd, SAVED_SCSIID, 0xFF);
  298. ahd_outb(ahd, SAVED_LUN, 0xFF);
  299. /*
  300. * Ensure that the sequencer's idea of TQINPOS
  301. * matches our own. The sequencer increments TQINPOS
  302. * only after it sees a DMA complete and a reset could
  303. * occur before the increment leaving the kernel to believe
  304. * the command arrived but the sequencer to not.
  305. */
  306. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  307. /* Always allow reselection */
  308. ahd_outb(ahd, SCSISEQ1,
  309. ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  310. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  311. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  312. ahd_unpause(ahd);
  313. }
  314. void
  315. ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
  316. {
  317. ahd_mode_state saved_modes;
  318. #ifdef AHD_DEBUG
  319. if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
  320. printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
  321. #endif
  322. saved_modes = ahd_save_modes(ahd);
  323. ahd_set_modes(ahd, fifo, fifo);
  324. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  325. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  326. ahd_outb(ahd, CCSGCTL, CCSGRESET);
  327. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  328. ahd_outb(ahd, SG_STATE, 0);
  329. ahd_restore_modes(ahd, saved_modes);
  330. }
  331. /************************* Input/Output Queues ********************************/
  332. /*
  333. * Flush and completed commands that are sitting in the command
  334. * complete queues down on the chip but have yet to be dma'ed back up.
  335. */
  336. void
  337. ahd_flush_qoutfifo(struct ahd_softc *ahd)
  338. {
  339. struct scb *scb;
  340. ahd_mode_state saved_modes;
  341. u_int saved_scbptr;
  342. u_int ccscbctl;
  343. u_int scbid;
  344. u_int next_scbid;
  345. saved_modes = ahd_save_modes(ahd);
  346. /*
  347. * Complete any SCBs that just finished being
  348. * DMA'ed into the qoutfifo.
  349. */
  350. ahd_run_qoutfifo(ahd);
  351. /*
  352. * Flush the good status FIFO for compelted packetized commands.
  353. */
  354. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  355. saved_scbptr = ahd_get_scbptr(ahd);
  356. while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
  357. u_int fifo_mode;
  358. u_int i;
  359. scbid = (ahd_inb(ahd, GSFIFO+1) << 8)
  360. | ahd_inb(ahd, GSFIFO);
  361. scb = ahd_lookup_scb(ahd, scbid);
  362. if (scb == NULL) {
  363. printf("%s: Warning - GSFIFO SCB %d invalid\n",
  364. ahd_name(ahd), scbid);
  365. continue;
  366. }
  367. /*
  368. * Determine if this transaction is still active in
  369. * any FIFO. If it is, we must flush that FIFO to
  370. * the host before completing the command.
  371. */
  372. fifo_mode = 0;
  373. for (i = 0; i < 2; i++) {
  374. /* Toggle to the other mode. */
  375. fifo_mode ^= 1;
  376. ahd_set_modes(ahd, fifo_mode, fifo_mode);
  377. if (ahd_scb_active_in_fifo(ahd, scb) == 0)
  378. continue;
  379. ahd_run_data_fifo(ahd, scb);
  380. /*
  381. * Clearing this transaction in this FIFO may
  382. * cause a CFG4DATA for this same transaction
  383. * to assert in the other FIFO. Make sure we
  384. * loop one more time and check the other FIFO.
  385. */
  386. i = 0;
  387. }
  388. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  389. ahd_set_scbptr(ahd, scbid);
  390. if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0
  391. && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0
  392. || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR)
  393. & SG_LIST_NULL) != 0)) {
  394. u_int comp_head;
  395. /*
  396. * The transfer completed with a residual.
  397. * Place this SCB on the complete DMA list
  398. * so that we Update our in-core copy of the
  399. * SCB before completing the command.
  400. */
  401. ahd_outb(ahd, SCB_SCSI_STATUS, 0);
  402. ahd_outb(ahd, SCB_SGPTR,
  403. ahd_inb_scbram(ahd, SCB_SGPTR)
  404. | SG_STATUS_VALID);
  405. ahd_outw(ahd, SCB_TAG, SCB_GET_TAG(scb));
  406. comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  407. ahd_outw(ahd, SCB_NEXT_COMPLETE, comp_head);
  408. if (SCBID_IS_NULL(comp_head))
  409. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD,
  410. SCB_GET_TAG(scb));
  411. } else
  412. ahd_complete_scb(ahd, scb);
  413. }
  414. ahd_set_scbptr(ahd, saved_scbptr);
  415. /*
  416. * Setup for command channel portion of flush.
  417. */
  418. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  419. /*
  420. * Wait for any inprogress DMA to complete and clear DMA state
  421. * if this if for an SCB in the qinfifo.
  422. */
  423. while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) {
  424. if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
  425. if ((ccscbctl & ARRDONE) != 0)
  426. break;
  427. } else if ((ccscbctl & CCSCBDONE) != 0)
  428. break;
  429. ahd_delay(200);
  430. }
  431. if ((ccscbctl & CCSCBDIR) != 0)
  432. ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
  433. saved_scbptr = ahd_get_scbptr(ahd);
  434. /*
  435. * Manually update/complete any completed SCBs that are waiting to be
  436. * DMA'ed back up to the host.
  437. */
  438. scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  439. while (!SCBID_IS_NULL(scbid)) {
  440. uint8_t *hscb_ptr;
  441. u_int i;
  442. ahd_set_scbptr(ahd, scbid);
  443. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  444. scb = ahd_lookup_scb(ahd, scbid);
  445. if (scb == NULL) {
  446. printf("%s: Warning - DMA-up and complete "
  447. "SCB %d invalid\n", ahd_name(ahd), scbid);
  448. continue;
  449. }
  450. hscb_ptr = (uint8_t *)scb->hscb;
  451. for (i = 0; i < sizeof(struct hardware_scb); i++)
  452. *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
  453. ahd_complete_scb(ahd, scb);
  454. scbid = next_scbid;
  455. }
  456. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  457. scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  458. while (!SCBID_IS_NULL(scbid)) {
  459. ahd_set_scbptr(ahd, scbid);
  460. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  461. scb = ahd_lookup_scb(ahd, scbid);
  462. if (scb == NULL) {
  463. printf("%s: Warning - Complete SCB %d invalid\n",
  464. ahd_name(ahd), scbid);
  465. continue;
  466. }
  467. ahd_complete_scb(ahd, scb);
  468. scbid = next_scbid;
  469. }
  470. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  471. /*
  472. * Restore state.
  473. */
  474. ahd_set_scbptr(ahd, saved_scbptr);
  475. ahd_restore_modes(ahd, saved_modes);
  476. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  477. }
  478. /*
  479. * Determine if an SCB for a packetized transaction
  480. * is active in a FIFO.
  481. */
  482. static int
  483. ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb)
  484. {
  485. /*
  486. * The FIFO is only active for our transaction if
  487. * the SCBPTR matches the SCB's ID and the firmware
  488. * has installed a handler for the FIFO or we have
  489. * a pending SAVEPTRS or CFG4DATA interrupt.
  490. */
  491. if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb)
  492. || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0
  493. && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0))
  494. return (0);
  495. return (1);
  496. }
  497. /*
  498. * Run a data fifo to completion for a transaction we know
  499. * has completed across the SCSI bus (good status has been
  500. * received). We are already set to the correct FIFO mode
  501. * on entry to this routine.
  502. *
  503. * This function attempts to operate exactly as the firmware
  504. * would when running this FIFO. Care must be taken to update
  505. * this routine any time the firmware's FIFO algorithm is
  506. * changed.
  507. */
  508. static void
  509. ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
  510. {
  511. u_int seqintsrc;
  512. while (1) {
  513. seqintsrc = ahd_inb(ahd, SEQINTSRC);
  514. if ((seqintsrc & CFG4DATA) != 0) {
  515. uint32_t datacnt;
  516. uint32_t sgptr;
  517. /*
  518. * Clear full residual flag.
  519. */
  520. sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
  521. ahd_outb(ahd, SCB_SGPTR, sgptr);
  522. /*
  523. * Load datacnt and address.
  524. */
  525. datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
  526. if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
  527. sgptr |= LAST_SEG;
  528. ahd_outb(ahd, SG_STATE, 0);
  529. } else
  530. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  531. ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
  532. ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
  533. ahd_outb(ahd, SG_CACHE_PRE, sgptr);
  534. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  535. /*
  536. * Initialize Residual Fields.
  537. */
  538. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
  539. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
  540. /*
  541. * Mark the SCB as having a FIFO in use.
  542. */
  543. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  544. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
  545. /*
  546. * Install a "fake" handler for this FIFO.
  547. */
  548. ahd_outw(ahd, LONGJMP_ADDR, 0);
  549. /*
  550. * Notify the hardware that we have satisfied
  551. * this sequencer interrupt.
  552. */
  553. ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
  554. } else if ((seqintsrc & SAVEPTRS) != 0) {
  555. uint32_t sgptr;
  556. uint32_t resid;
  557. if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
  558. /*
  559. * Snapshot Save Pointers. Clear
  560. * the snapshot and continue.
  561. */
  562. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  563. continue;
  564. }
  565. /*
  566. * Disable S/G fetch so the DMA engine
  567. * is available to future users.
  568. */
  569. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  570. ahd_outb(ahd, CCSGCTL, 0);
  571. ahd_outb(ahd, SG_STATE, 0);
  572. /*
  573. * Flush the data FIFO. Strickly only
  574. * necessary for Rev A parts.
  575. */
  576. ahd_outb(ahd, DFCNTRL,
  577. ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
  578. /*
  579. * Calculate residual.
  580. */
  581. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  582. resid = ahd_inl(ahd, SHCNT);
  583. resid |=
  584. ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
  585. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
  586. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
  587. /*
  588. * Must back up to the correct S/G element.
  589. * Typically this just means resetting our
  590. * low byte to the offset in the SG_CACHE,
  591. * but if we wrapped, we have to correct
  592. * the other bytes of the sgptr too.
  593. */
  594. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
  595. && (sgptr & 0x80) == 0)
  596. sgptr -= 0x100;
  597. sgptr &= ~0xFF;
  598. sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
  599. & SG_ADDR_MASK;
  600. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  601. ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
  602. } else if ((resid & AHD_SG_LEN_MASK) == 0) {
  603. ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
  604. sgptr | SG_LIST_NULL);
  605. }
  606. /*
  607. * Save Pointers.
  608. */
  609. ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
  610. ahd_outl(ahd, SCB_DATACNT, resid);
  611. ahd_outl(ahd, SCB_SGPTR, sgptr);
  612. ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
  613. ahd_outb(ahd, SEQIMODE,
  614. ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
  615. /*
  616. * If the data is to the SCSI bus, we are
  617. * done, otherwise wait for FIFOEMP.
  618. */
  619. if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
  620. break;
  621. } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
  622. uint32_t sgptr;
  623. uint64_t data_addr;
  624. uint32_t data_len;
  625. u_int dfcntrl;
  626. /*
  627. * Disable S/G fetch so the DMA engine
  628. * is available to future users.
  629. */
  630. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
  631. ahd_outb(ahd, CCSGCTL, 0);
  632. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  633. }
  634. /*
  635. * Wait for the DMA engine to notice that the
  636. * host transfer is enabled and that there is
  637. * space in the S/G FIFO for new segments before
  638. * loading more segments.
  639. */
  640. if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) == 0)
  641. continue;
  642. if ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) == 0)
  643. continue;
  644. /*
  645. * Determine the offset of the next S/G
  646. * element to load.
  647. */
  648. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  649. sgptr &= SG_PTR_MASK;
  650. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  651. struct ahd_dma64_seg *sg;
  652. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  653. data_addr = sg->addr;
  654. data_len = sg->len;
  655. sgptr += sizeof(*sg);
  656. } else {
  657. struct ahd_dma_seg *sg;
  658. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  659. data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK;
  660. data_addr <<= 8;
  661. data_addr |= sg->addr;
  662. data_len = sg->len;
  663. sgptr += sizeof(*sg);
  664. }
  665. /*
  666. * Update residual information.
  667. */
  668. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24);
  669. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  670. /*
  671. * Load the S/G.
  672. */
  673. if (data_len & AHD_DMA_LAST_SEG) {
  674. sgptr |= LAST_SEG;
  675. ahd_outb(ahd, SG_STATE, 0);
  676. }
  677. ahd_outq(ahd, HADDR, data_addr);
  678. ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK);
  679. ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF);
  680. /*
  681. * Advertise the segment to the hardware.
  682. */
  683. dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
  684. if ((ahd->features & AHD_NEW_DFCNTRL_OPTS)!=0) {
  685. /*
  686. * Use SCSIENWRDIS so that SCSIEN
  687. * is never modified by this
  688. * operation.
  689. */
  690. dfcntrl |= SCSIENWRDIS;
  691. }
  692. ahd_outb(ahd, DFCNTRL, dfcntrl);
  693. } else if ((ahd_inb(ahd, SG_CACHE_SHADOW)
  694. & LAST_SEG_DONE) != 0) {
  695. /*
  696. * Transfer completed to the end of SG list
  697. * and has flushed to the host.
  698. */
  699. ahd_outb(ahd, SCB_SGPTR,
  700. ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
  701. break;
  702. } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
  703. break;
  704. }
  705. ahd_delay(200);
  706. }
  707. /*
  708. * Clear any handler for this FIFO, decrement
  709. * the FIFO use count for the SCB, and release
  710. * the FIFO.
  711. */
  712. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  713. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  714. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
  715. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  716. }
  717. void
  718. ahd_run_qoutfifo(struct ahd_softc *ahd)
  719. {
  720. struct scb *scb;
  721. u_int scb_index;
  722. if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
  723. panic("ahd_run_qoutfifo recursion");
  724. ahd->flags |= AHD_RUNNING_QOUTFIFO;
  725. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
  726. while ((ahd->qoutfifo[ahd->qoutfifonext]
  727. & QOUTFIFO_ENTRY_VALID_LE) == ahd->qoutfifonext_valid_tag) {
  728. scb_index = ahd_le16toh(ahd->qoutfifo[ahd->qoutfifonext]
  729. & ~QOUTFIFO_ENTRY_VALID_LE);
  730. scb = ahd_lookup_scb(ahd, scb_index);
  731. if (scb == NULL) {
  732. printf("%s: WARNING no command for scb %d "
  733. "(cmdcmplt)\nQOUTPOS = %d\n",
  734. ahd_name(ahd), scb_index,
  735. ahd->qoutfifonext);
  736. ahd_dump_card_state(ahd);
  737. } else
  738. ahd_complete_scb(ahd, scb);
  739. ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
  740. if (ahd->qoutfifonext == 0)
  741. ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID_LE;
  742. }
  743. ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
  744. }
  745. /************************* Interrupt Handling *********************************/
  746. void
  747. ahd_handle_hwerrint(struct ahd_softc *ahd)
  748. {
  749. /*
  750. * Some catastrophic hardware error has occurred.
  751. * Print it for the user and disable the controller.
  752. */
  753. int i;
  754. int error;
  755. error = ahd_inb(ahd, ERROR);
  756. for (i = 0; i < num_errors; i++) {
  757. if ((error & ahd_hard_errors[i].errno) != 0)
  758. printf("%s: hwerrint, %s\n",
  759. ahd_name(ahd), ahd_hard_errors[i].errmesg);
  760. }
  761. ahd_dump_card_state(ahd);
  762. panic("BRKADRINT");
  763. /* Tell everyone that this HBA is no longer available */
  764. ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  765. CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  766. CAM_NO_HBA);
  767. /* Tell the system that this controller has gone away. */
  768. ahd_free(ahd);
  769. }
  770. void
  771. ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
  772. {
  773. u_int seqintcode;
  774. /*
  775. * Save the sequencer interrupt code and clear the SEQINT
  776. * bit. We will unpause the sequencer, if appropriate,
  777. * after servicing the request.
  778. */
  779. seqintcode = ahd_inb(ahd, SEQINTCODE);
  780. ahd_outb(ahd, CLRINT, CLRSEQINT);
  781. if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
  782. /*
  783. * Unpause the sequencer and let it clear
  784. * SEQINT by writing NO_SEQINT to it. This
  785. * will cause the sequencer to be paused again,
  786. * which is the expected state of this routine.
  787. */
  788. ahd_unpause(ahd);
  789. while (!ahd_is_paused(ahd))
  790. ;
  791. ahd_outb(ahd, CLRINT, CLRSEQINT);
  792. }
  793. ahd_update_modes(ahd);
  794. #ifdef AHD_DEBUG
  795. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  796. printf("%s: Handle Seqint Called for code %d\n",
  797. ahd_name(ahd), seqintcode);
  798. #endif
  799. switch (seqintcode) {
  800. case BAD_SCB_STATUS:
  801. {
  802. struct scb *scb;
  803. u_int scbid;
  804. int cmds_pending;
  805. scbid = ahd_get_scbptr(ahd);
  806. scb = ahd_lookup_scb(ahd, scbid);
  807. if (scb != NULL) {
  808. ahd_complete_scb(ahd, scb);
  809. } else {
  810. printf("%s: WARNING no command for scb %d "
  811. "(bad status)\n", ahd_name(ahd), scbid);
  812. ahd_dump_card_state(ahd);
  813. }
  814. cmds_pending = ahd_inw(ahd, CMDS_PENDING);
  815. if (cmds_pending > 0)
  816. ahd_outw(ahd, CMDS_PENDING, cmds_pending - 1);
  817. break;
  818. }
  819. case ENTERING_NONPACK:
  820. {
  821. struct scb *scb;
  822. u_int scbid;
  823. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  824. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  825. scbid = ahd_get_scbptr(ahd);
  826. scb = ahd_lookup_scb(ahd, scbid);
  827. if (scb == NULL) {
  828. /*
  829. * Somehow need to know if this
  830. * is from a selection or reselection.
  831. * From that, we can determine target
  832. * ID so we at least have an I_T nexus.
  833. */
  834. } else {
  835. ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
  836. ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
  837. ahd_outb(ahd, SEQ_FLAGS, 0x0);
  838. }
  839. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
  840. && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  841. /*
  842. * Phase change after read stream with
  843. * CRC error with P0 asserted on last
  844. * packet.
  845. */
  846. #ifdef AHD_DEBUG
  847. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  848. printf("%s: Assuming LQIPHASE_NLQ with "
  849. "P0 assertion\n", ahd_name(ahd));
  850. #endif
  851. }
  852. #ifdef AHD_DEBUG
  853. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  854. printf("%s: Entering NONPACK\n", ahd_name(ahd));
  855. #endif
  856. break;
  857. }
  858. case INVALID_SEQINT:
  859. printf("%s: Invalid Sequencer interrupt occurred.\n",
  860. ahd_name(ahd));
  861. ahd_dump_card_state(ahd);
  862. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  863. break;
  864. case STATUS_OVERRUN:
  865. {
  866. struct scb *scb;
  867. u_int scbid;
  868. scbid = ahd_get_scbptr(ahd);
  869. scb = ahd_lookup_scb(ahd, scbid);
  870. if (scb != NULL)
  871. ahd_print_path(ahd, scb);
  872. else
  873. printf("%s: ", ahd_name(ahd));
  874. printf("SCB %d Packetized Status Overrun", scbid);
  875. ahd_dump_card_state(ahd);
  876. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  877. break;
  878. }
  879. case CFG4ISTAT_INTR:
  880. {
  881. struct scb *scb;
  882. u_int scbid;
  883. scbid = ahd_get_scbptr(ahd);
  884. scb = ahd_lookup_scb(ahd, scbid);
  885. if (scb == NULL) {
  886. ahd_dump_card_state(ahd);
  887. printf("CFG4ISTAT: Free SCB %d referenced", scbid);
  888. panic("For safety");
  889. }
  890. ahd_outq(ahd, HADDR, scb->sense_busaddr);
  891. ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
  892. ahd_outb(ahd, HCNT + 2, 0);
  893. ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
  894. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  895. break;
  896. }
  897. case ILLEGAL_PHASE:
  898. {
  899. u_int bus_phase;
  900. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  901. printf("%s: ILLEGAL_PHASE 0x%x\n",
  902. ahd_name(ahd), bus_phase);
  903. switch (bus_phase) {
  904. case P_DATAOUT:
  905. case P_DATAIN:
  906. case P_DATAOUT_DT:
  907. case P_DATAIN_DT:
  908. case P_MESGOUT:
  909. case P_STATUS:
  910. case P_MESGIN:
  911. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  912. printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
  913. break;
  914. case P_COMMAND:
  915. {
  916. struct ahd_devinfo devinfo;
  917. struct scb *scb;
  918. struct ahd_initiator_tinfo *targ_info;
  919. struct ahd_tmode_tstate *tstate;
  920. struct ahd_transinfo *tinfo;
  921. u_int scbid;
  922. /*
  923. * If a target takes us into the command phase
  924. * assume that it has been externally reset and
  925. * has thus lost our previous packetized negotiation
  926. * agreement. Since we have not sent an identify
  927. * message and may not have fully qualified the
  928. * connection, we change our command to TUR, assert
  929. * ATN and ABORT the task when we go to message in
  930. * phase. The OSM will see the REQUEUE_REQUEST
  931. * status and retry the command.
  932. */
  933. scbid = ahd_get_scbptr(ahd);
  934. scb = ahd_lookup_scb(ahd, scbid);
  935. if (scb == NULL) {
  936. printf("Invalid phase with no valid SCB. "
  937. "Resetting bus.\n");
  938. ahd_reset_channel(ahd, 'A',
  939. /*Initiate Reset*/TRUE);
  940. break;
  941. }
  942. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  943. SCB_GET_TARGET(ahd, scb),
  944. SCB_GET_LUN(scb),
  945. SCB_GET_CHANNEL(ahd, scb),
  946. ROLE_INITIATOR);
  947. targ_info = ahd_fetch_transinfo(ahd,
  948. devinfo.channel,
  949. devinfo.our_scsiid,
  950. devinfo.target,
  951. &tstate);
  952. tinfo = &targ_info->curr;
  953. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  954. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  955. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  956. /*offset*/0, /*ppr_options*/0,
  957. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  958. ahd_outb(ahd, SCB_CDB_STORE, 0);
  959. ahd_outb(ahd, SCB_CDB_STORE+1, 0);
  960. ahd_outb(ahd, SCB_CDB_STORE+2, 0);
  961. ahd_outb(ahd, SCB_CDB_STORE+3, 0);
  962. ahd_outb(ahd, SCB_CDB_STORE+4, 0);
  963. ahd_outb(ahd, SCB_CDB_STORE+5, 0);
  964. ahd_outb(ahd, SCB_CDB_LEN, 6);
  965. scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
  966. scb->hscb->control |= MK_MESSAGE;
  967. ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
  968. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  969. ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
  970. /*
  971. * The lun is 0, regardless of the SCB's lun
  972. * as we have not sent an identify message.
  973. */
  974. ahd_outb(ahd, SAVED_LUN, 0);
  975. ahd_outb(ahd, SEQ_FLAGS, 0);
  976. ahd_assert_atn(ahd);
  977. scb->flags &= ~(SCB_PACKETIZED);
  978. scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
  979. ahd_freeze_devq(ahd, scb);
  980. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  981. ahd_freeze_scb(scb);
  982. /*
  983. * Allow the sequencer to continue with
  984. * non-pack processing.
  985. */
  986. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  987. ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
  988. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  989. ahd_outb(ahd, CLRLQOINT1, 0);
  990. }
  991. #ifdef AHD_DEBUG
  992. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  993. ahd_print_path(ahd, scb);
  994. printf("Unexpected command phase from "
  995. "packetized target\n");
  996. }
  997. #endif
  998. break;
  999. }
  1000. }
  1001. break;
  1002. }
  1003. case CFG4OVERRUN:
  1004. {
  1005. struct scb *scb;
  1006. u_int scb_index;
  1007. #ifdef AHD_DEBUG
  1008. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1009. printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
  1010. ahd_inb(ahd, MODE_PTR));
  1011. }
  1012. #endif
  1013. scb_index = ahd_get_scbptr(ahd);
  1014. scb = ahd_lookup_scb(ahd, scb_index);
  1015. if (scb == NULL) {
  1016. /*
  1017. * Attempt to transfer to an SCB that is
  1018. * not outstanding.
  1019. */
  1020. ahd_assert_atn(ahd);
  1021. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1022. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  1023. ahd->msgout_len = 1;
  1024. ahd->msgout_index = 0;
  1025. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1026. /*
  1027. * Clear status received flag to prevent any
  1028. * attempt to complete this bogus SCB.
  1029. */
  1030. ahd_outb(ahd, SCB_CONTROL,
  1031. ahd_inb_scbram(ahd, SCB_CONTROL)
  1032. & ~STATUS_RCVD);
  1033. }
  1034. break;
  1035. }
  1036. case DUMP_CARD_STATE:
  1037. {
  1038. ahd_dump_card_state(ahd);
  1039. break;
  1040. }
  1041. case PDATA_REINIT:
  1042. {
  1043. #ifdef AHD_DEBUG
  1044. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1045. printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
  1046. "SG_CACHE_SHADOW = 0x%x\n",
  1047. ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
  1048. ahd_inb(ahd, SG_CACHE_SHADOW));
  1049. }
  1050. #endif
  1051. ahd_reinitialize_dataptrs(ahd);
  1052. break;
  1053. }
  1054. case HOST_MSG_LOOP:
  1055. {
  1056. struct ahd_devinfo devinfo;
  1057. /*
  1058. * The sequencer has encountered a message phase
  1059. * that requires host assistance for completion.
  1060. * While handling the message phase(s), we will be
  1061. * notified by the sequencer after each byte is
  1062. * transfered so we can track bus phase changes.
  1063. *
  1064. * If this is the first time we've seen a HOST_MSG_LOOP
  1065. * interrupt, initialize the state of the host message
  1066. * loop.
  1067. */
  1068. ahd_fetch_devinfo(ahd, &devinfo);
  1069. if (ahd->msg_type == MSG_TYPE_NONE) {
  1070. struct scb *scb;
  1071. u_int scb_index;
  1072. u_int bus_phase;
  1073. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1074. if (bus_phase != P_MESGIN
  1075. && bus_phase != P_MESGOUT) {
  1076. printf("ahd_intr: HOST_MSG_LOOP bad "
  1077. "phase 0x%x\n", bus_phase);
  1078. /*
  1079. * Probably transitioned to bus free before
  1080. * we got here. Just punt the message.
  1081. */
  1082. ahd_dump_card_state(ahd);
  1083. ahd_clear_intstat(ahd);
  1084. ahd_restart(ahd);
  1085. return;
  1086. }
  1087. scb_index = ahd_get_scbptr(ahd);
  1088. scb = ahd_lookup_scb(ahd, scb_index);
  1089. if (devinfo.role == ROLE_INITIATOR) {
  1090. if (bus_phase == P_MESGOUT)
  1091. ahd_setup_initiator_msgout(ahd,
  1092. &devinfo,
  1093. scb);
  1094. else {
  1095. ahd->msg_type =
  1096. MSG_TYPE_INITIATOR_MSGIN;
  1097. ahd->msgin_index = 0;
  1098. }
  1099. }
  1100. #ifdef AHD_TARGET_MODE
  1101. else {
  1102. if (bus_phase == P_MESGOUT) {
  1103. ahd->msg_type =
  1104. MSG_TYPE_TARGET_MSGOUT;
  1105. ahd->msgin_index = 0;
  1106. }
  1107. else
  1108. ahd_setup_target_msgin(ahd,
  1109. &devinfo,
  1110. scb);
  1111. }
  1112. #endif
  1113. }
  1114. ahd_handle_message_phase(ahd);
  1115. break;
  1116. }
  1117. case NO_MATCH:
  1118. {
  1119. /* Ensure we don't leave the selection hardware on */
  1120. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  1121. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1122. printf("%s:%c:%d: no active SCB for reconnecting "
  1123. "target - issuing BUS DEVICE RESET\n",
  1124. ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
  1125. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  1126. "REG0 == 0x%x ACCUM = 0x%x\n",
  1127. ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
  1128. ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
  1129. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  1130. "SINDEX == 0x%x\n",
  1131. ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
  1132. ahd_find_busy_tcl(ahd,
  1133. BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
  1134. ahd_inb(ahd, SAVED_LUN))),
  1135. ahd_inw(ahd, SINDEX));
  1136. printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  1137. "SCB_CONTROL == 0x%x\n",
  1138. ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
  1139. ahd_inb_scbram(ahd, SCB_LUN),
  1140. ahd_inb_scbram(ahd, SCB_CONTROL));
  1141. printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
  1142. ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
  1143. printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
  1144. printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
  1145. ahd_dump_card_state(ahd);
  1146. ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
  1147. ahd->msgout_len = 1;
  1148. ahd->msgout_index = 0;
  1149. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1150. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1151. ahd_assert_atn(ahd);
  1152. break;
  1153. }
  1154. case PROTO_VIOLATION:
  1155. {
  1156. ahd_handle_proto_violation(ahd);
  1157. break;
  1158. }
  1159. case IGN_WIDE_RES:
  1160. {
  1161. struct ahd_devinfo devinfo;
  1162. ahd_fetch_devinfo(ahd, &devinfo);
  1163. ahd_handle_ign_wide_residue(ahd, &devinfo);
  1164. break;
  1165. }
  1166. case BAD_PHASE:
  1167. {
  1168. u_int lastphase;
  1169. lastphase = ahd_inb(ahd, LASTPHASE);
  1170. printf("%s:%c:%d: unknown scsi bus phase %x, "
  1171. "lastphase = 0x%x. Attempting to continue\n",
  1172. ahd_name(ahd), 'A',
  1173. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1174. lastphase, ahd_inb(ahd, SCSISIGI));
  1175. break;
  1176. }
  1177. case MISSED_BUSFREE:
  1178. {
  1179. u_int lastphase;
  1180. lastphase = ahd_inb(ahd, LASTPHASE);
  1181. printf("%s:%c:%d: Missed busfree. "
  1182. "Lastphase = 0x%x, Curphase = 0x%x\n",
  1183. ahd_name(ahd), 'A',
  1184. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1185. lastphase, ahd_inb(ahd, SCSISIGI));
  1186. ahd_restart(ahd);
  1187. return;
  1188. }
  1189. case DATA_OVERRUN:
  1190. {
  1191. /*
  1192. * When the sequencer detects an overrun, it
  1193. * places the controller in "BITBUCKET" mode
  1194. * and allows the target to complete its transfer.
  1195. * Unfortunately, none of the counters get updated
  1196. * when the controller is in this mode, so we have
  1197. * no way of knowing how large the overrun was.
  1198. */
  1199. struct scb *scb;
  1200. u_int scbindex;
  1201. #ifdef AHD_DEBUG
  1202. u_int lastphase;
  1203. #endif
  1204. scbindex = ahd_get_scbptr(ahd);
  1205. scb = ahd_lookup_scb(ahd, scbindex);
  1206. #ifdef AHD_DEBUG
  1207. lastphase = ahd_inb(ahd, LASTPHASE);
  1208. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1209. ahd_print_path(ahd, scb);
  1210. printf("data overrun detected %s. Tag == 0x%x.\n",
  1211. ahd_lookup_phase_entry(lastphase)->phasemsg,
  1212. SCB_GET_TAG(scb));
  1213. ahd_print_path(ahd, scb);
  1214. printf("%s seen Data Phase. Length = %ld. "
  1215. "NumSGs = %d.\n",
  1216. ahd_inb(ahd, SEQ_FLAGS) & DPHASE
  1217. ? "Have" : "Haven't",
  1218. ahd_get_transfer_length(scb), scb->sg_count);
  1219. ahd_dump_sglist(scb);
  1220. }
  1221. #endif
  1222. /*
  1223. * Set this and it will take effect when the
  1224. * target does a command complete.
  1225. */
  1226. ahd_freeze_devq(ahd, scb);
  1227. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1228. ahd_freeze_scb(scb);
  1229. break;
  1230. }
  1231. case MKMSG_FAILED:
  1232. {
  1233. struct ahd_devinfo devinfo;
  1234. struct scb *scb;
  1235. u_int scbid;
  1236. ahd_fetch_devinfo(ahd, &devinfo);
  1237. printf("%s:%c:%d:%d: Attempt to issue message failed\n",
  1238. ahd_name(ahd), devinfo.channel, devinfo.target,
  1239. devinfo.lun);
  1240. scbid = ahd_get_scbptr(ahd);
  1241. scb = ahd_lookup_scb(ahd, scbid);
  1242. if (scb != NULL
  1243. && (scb->flags & SCB_RECOVERY_SCB) != 0)
  1244. /*
  1245. * Ensure that we didn't put a second instance of this
  1246. * SCB into the QINFIFO.
  1247. */
  1248. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1249. SCB_GET_CHANNEL(ahd, scb),
  1250. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1251. ROLE_INITIATOR, /*status*/0,
  1252. SEARCH_REMOVE);
  1253. ahd_outb(ahd, SCB_CONTROL,
  1254. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  1255. break;
  1256. }
  1257. case TASKMGMT_FUNC_COMPLETE:
  1258. {
  1259. u_int scbid;
  1260. struct scb *scb;
  1261. scbid = ahd_get_scbptr(ahd);
  1262. scb = ahd_lookup_scb(ahd, scbid);
  1263. if (scb != NULL) {
  1264. u_int lun;
  1265. u_int tag;
  1266. cam_status error;
  1267. ahd_print_path(ahd, scb);
  1268. printf("Task Management Func 0x%x Complete\n",
  1269. scb->hscb->task_management);
  1270. lun = CAM_LUN_WILDCARD;
  1271. tag = SCB_LIST_NULL;
  1272. switch (scb->hscb->task_management) {
  1273. case SIU_TASKMGMT_ABORT_TASK:
  1274. tag = SCB_GET_TAG(scb);
  1275. case SIU_TASKMGMT_ABORT_TASK_SET:
  1276. case SIU_TASKMGMT_CLEAR_TASK_SET:
  1277. lun = scb->hscb->lun;
  1278. error = CAM_REQ_ABORTED;
  1279. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  1280. 'A', lun, tag, ROLE_INITIATOR,
  1281. error);
  1282. break;
  1283. case SIU_TASKMGMT_LUN_RESET:
  1284. lun = scb->hscb->lun;
  1285. case SIU_TASKMGMT_TARGET_RESET:
  1286. {
  1287. struct ahd_devinfo devinfo;
  1288. ahd_scb_devinfo(ahd, &devinfo, scb);
  1289. error = CAM_BDR_SENT;
  1290. ahd_handle_devreset(ahd, &devinfo, lun,
  1291. CAM_BDR_SENT,
  1292. lun != CAM_LUN_WILDCARD
  1293. ? "Lun Reset"
  1294. : "Target Reset",
  1295. /*verbose_level*/0);
  1296. break;
  1297. }
  1298. default:
  1299. panic("Unexpected TaskMgmt Func\n");
  1300. break;
  1301. }
  1302. }
  1303. break;
  1304. }
  1305. case TASKMGMT_CMD_CMPLT_OKAY:
  1306. {
  1307. u_int scbid;
  1308. struct scb *scb;
  1309. /*
  1310. * An ABORT TASK TMF failed to be delivered before
  1311. * the targeted command completed normally.
  1312. */
  1313. scbid = ahd_get_scbptr(ahd);
  1314. scb = ahd_lookup_scb(ahd, scbid);
  1315. if (scb != NULL) {
  1316. /*
  1317. * Remove the second instance of this SCB from
  1318. * the QINFIFO if it is still there.
  1319. */
  1320. ahd_print_path(ahd, scb);
  1321. printf("SCB completes before TMF\n");
  1322. /*
  1323. * Handle losing the race. Wait until any
  1324. * current selection completes. We will then
  1325. * set the TMF back to zero in this SCB so that
  1326. * the sequencer doesn't bother to issue another
  1327. * sequencer interrupt for its completion.
  1328. */
  1329. while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  1330. && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
  1331. && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
  1332. ;
  1333. ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
  1334. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1335. SCB_GET_CHANNEL(ahd, scb),
  1336. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1337. ROLE_INITIATOR, /*status*/0,
  1338. SEARCH_REMOVE);
  1339. }
  1340. break;
  1341. }
  1342. case TRACEPOINT0:
  1343. case TRACEPOINT1:
  1344. case TRACEPOINT2:
  1345. case TRACEPOINT3:
  1346. printf("%s: Tracepoint %d\n", ahd_name(ahd),
  1347. seqintcode - TRACEPOINT0);
  1348. break;
  1349. case NO_SEQINT:
  1350. break;
  1351. case SAW_HWERR:
  1352. ahd_handle_hwerrint(ahd);
  1353. break;
  1354. default:
  1355. printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
  1356. seqintcode);
  1357. break;
  1358. }
  1359. /*
  1360. * The sequencer is paused immediately on
  1361. * a SEQINT, so we should restart it when
  1362. * we're done.
  1363. */
  1364. ahd_unpause(ahd);
  1365. }
  1366. void
  1367. ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
  1368. {
  1369. struct scb *scb;
  1370. u_int status0;
  1371. u_int status3;
  1372. u_int status;
  1373. u_int lqistat1;
  1374. u_int lqostat0;
  1375. u_int scbid;
  1376. u_int busfreetime;
  1377. ahd_update_modes(ahd);
  1378. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1379. status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
  1380. status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
  1381. status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  1382. lqistat1 = ahd_inb(ahd, LQISTAT1);
  1383. lqostat0 = ahd_inb(ahd, LQOSTAT0);
  1384. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1385. if ((status0 & (SELDI|SELDO)) != 0) {
  1386. u_int simode0;
  1387. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1388. simode0 = ahd_inb(ahd, SIMODE0);
  1389. status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
  1390. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1391. }
  1392. scbid = ahd_get_scbptr(ahd);
  1393. scb = ahd_lookup_scb(ahd, scbid);
  1394. if (scb != NULL
  1395. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  1396. scb = NULL;
  1397. /* Make sure the sequencer is in a safe location. */
  1398. ahd_clear_critical_section(ahd);
  1399. if ((status0 & IOERR) != 0) {
  1400. u_int now_lvd;
  1401. now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
  1402. printf("%s: Transceiver State Has Changed to %s mode\n",
  1403. ahd_name(ahd), now_lvd ? "LVD" : "SE");
  1404. ahd_outb(ahd, CLRSINT0, CLRIOERR);
  1405. /*
  1406. * A change in I/O mode is equivalent to a bus reset.
  1407. */
  1408. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1409. ahd_pause(ahd);
  1410. ahd_setup_iocell_workaround(ahd);
  1411. ahd_unpause(ahd);
  1412. } else if ((status0 & OVERRUN) != 0) {
  1413. printf("%s: SCSI offset overrun detected. Resetting bus.\n",
  1414. ahd_name(ahd));
  1415. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1416. } else if ((status & SCSIRSTI) != 0) {
  1417. printf("%s: Someone reset channel A\n", ahd_name(ahd));
  1418. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
  1419. } else if ((status & SCSIPERR) != 0) {
  1420. ahd_handle_transmission_error(ahd);
  1421. } else if (lqostat0 != 0) {
  1422. printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
  1423. ahd_outb(ahd, CLRLQOINT0, lqostat0);
  1424. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  1425. ahd_outb(ahd, CLRLQOINT1, 0);
  1426. }
  1427. } else if ((status & SELTO) != 0) {
  1428. u_int scbid;
  1429. /* Stop the selection */
  1430. ahd_outb(ahd, SCSISEQ0, 0);
  1431. /* No more pending messages */
  1432. ahd_clear_msg_state(ahd);
  1433. /* Clear interrupt state */
  1434. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1435. /*
  1436. * Although the driver does not care about the
  1437. * 'Selection in Progress' status bit, the busy
  1438. * LED does. SELINGO is only cleared by a sucessfull
  1439. * selection, so we must manually clear it to insure
  1440. * the LED turns off just incase no future successful
  1441. * selections occur (e.g. no devices on the bus).
  1442. */
  1443. ahd_outb(ahd, CLRSINT0, CLRSELINGO);
  1444. scbid = ahd_inw(ahd, WAITING_TID_HEAD);
  1445. scb = ahd_lookup_scb(ahd, scbid);
  1446. if (scb == NULL) {
  1447. printf("%s: ahd_intr - referenced scb not "
  1448. "valid during SELTO scb(0x%x)\n",
  1449. ahd_name(ahd), scbid);
  1450. ahd_dump_card_state(ahd);
  1451. } else {
  1452. struct ahd_devinfo devinfo;
  1453. #ifdef AHD_DEBUG
  1454. if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
  1455. ahd_print_path(ahd, scb);
  1456. printf("Saw Selection Timeout for SCB 0x%x\n",
  1457. scbid);
  1458. }
  1459. #endif
  1460. /*
  1461. * Force a renegotiation with this target just in
  1462. * case the cable was pulled and will later be
  1463. * re-attached. The target may forget its negotiation
  1464. * settings with us should it attempt to reselect
  1465. * during the interruption. The target will not issue
  1466. * a unit attention in this case, so we must always
  1467. * renegotiate.
  1468. */
  1469. ahd_scb_devinfo(ahd, &devinfo, scb);
  1470. ahd_force_renegotiation(ahd, &devinfo);
  1471. ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1472. ahd_freeze_devq(ahd, scb);
  1473. }
  1474. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1475. ahd_iocell_first_selection(ahd);
  1476. ahd_unpause(ahd);
  1477. } else if ((status0 & (SELDI|SELDO)) != 0) {
  1478. ahd_iocell_first_selection(ahd);
  1479. ahd_unpause(ahd);
  1480. } else if (status3 != 0) {
  1481. printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
  1482. ahd_name(ahd), status3);
  1483. ahd_outb(ahd, CLRSINT3, status3);
  1484. } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
  1485. ahd_handle_lqiphase_error(ahd, lqistat1);
  1486. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1487. /*
  1488. * This status can be delayed during some
  1489. * streaming operations. The SCSIPHASE
  1490. * handler has already dealt with this case
  1491. * so just clear the error.
  1492. */
  1493. ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
  1494. } else if ((status & BUSFREE) != 0) {
  1495. u_int lqostat1;
  1496. int restart;
  1497. int clear_fifo;
  1498. int packetized;
  1499. u_int mode;
  1500. /*
  1501. * Clear our selection hardware as soon as possible.
  1502. * We may have an entry in the waiting Q for this target,
  1503. * that is affected by this busfree and we don't want to
  1504. * go about selecting the target while we handle the event.
  1505. */
  1506. ahd_outb(ahd, SCSISEQ0, 0);
  1507. /*
  1508. * Determine what we were up to at the time of
  1509. * the busfree.
  1510. */
  1511. mode = AHD_MODE_SCSI;
  1512. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1513. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1514. switch (busfreetime) {
  1515. case BUSFREE_DFF0:
  1516. case BUSFREE_DFF1:
  1517. {
  1518. u_int scbid;
  1519. struct scb *scb;
  1520. mode = busfreetime == BUSFREE_DFF0
  1521. ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
  1522. ahd_set_modes(ahd, mode, mode);
  1523. scbid = ahd_get_scbptr(ahd);
  1524. scb = ahd_lookup_scb(ahd, scbid);
  1525. if (scb == NULL) {
  1526. printf("%s: Invalid SCB %d in DFF%d "
  1527. "during unexpected busfree\n",
  1528. ahd_name(ahd), scbid, mode);
  1529. packetized = 0;
  1530. } else
  1531. packetized = (scb->flags & SCB_PACKETIZED) != 0;
  1532. clear_fifo = 1;
  1533. break;
  1534. }
  1535. case BUSFREE_LQO:
  1536. clear_fifo = 0;
  1537. packetized = 1;
  1538. break;
  1539. default:
  1540. clear_fifo = 0;
  1541. packetized = (lqostat1 & LQOBUSFREE) != 0;
  1542. if (!packetized
  1543. && ahd_inb(ahd, LASTPHASE) == P_BUSFREE)
  1544. packetized = 1;
  1545. break;
  1546. }
  1547. #ifdef AHD_DEBUG
  1548. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  1549. printf("Saw Busfree. Busfreetime = 0x%x.\n",
  1550. busfreetime);
  1551. #endif
  1552. /*
  1553. * Busfrees that occur in non-packetized phases are
  1554. * handled by the nonpkt_busfree handler.
  1555. */
  1556. if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
  1557. restart = ahd_handle_pkt_busfree(ahd, busfreetime);
  1558. } else {
  1559. packetized = 0;
  1560. restart = ahd_handle_nonpkt_busfree(ahd);
  1561. }
  1562. /*
  1563. * Clear the busfree interrupt status. The setting of
  1564. * the interrupt is a pulse, so in a perfect world, we
  1565. * would not need to muck with the ENBUSFREE logic. This
  1566. * would ensure that if the bus moves on to another
  1567. * connection, busfree protection is still in force. If
  1568. * BUSFREEREV is broken, however, we must manually clear
  1569. * the ENBUSFREE if the busfree occurred during a non-pack
  1570. * connection so that we don't get false positives during
  1571. * future, packetized, connections.
  1572. */
  1573. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  1574. if (packetized == 0
  1575. && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
  1576. ahd_outb(ahd, SIMODE1,
  1577. ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
  1578. if (clear_fifo)
  1579. ahd_clear_fifo(ahd, mode);
  1580. ahd_clear_msg_state(ahd);
  1581. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1582. if (restart) {
  1583. ahd_restart(ahd);
  1584. } else {
  1585. ahd_unpause(ahd);
  1586. }
  1587. } else {
  1588. printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
  1589. ahd_name(ahd), status);
  1590. ahd_dump_card_state(ahd);
  1591. ahd_clear_intstat(ahd);
  1592. ahd_unpause(ahd);
  1593. }
  1594. }
  1595. static void
  1596. ahd_handle_transmission_error(struct ahd_softc *ahd)
  1597. {
  1598. struct scb *scb;
  1599. u_int scbid;
  1600. u_int lqistat1;
  1601. u_int lqistat2;
  1602. u_int msg_out;
  1603. u_int curphase;
  1604. u_int lastphase;
  1605. u_int perrdiag;
  1606. u_int cur_col;
  1607. int silent;
  1608. scb = NULL;
  1609. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1610. lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
  1611. lqistat2 = ahd_inb(ahd, LQISTAT2);
  1612. if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
  1613. && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
  1614. u_int lqistate;
  1615. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1616. lqistate = ahd_inb(ahd, LQISTATE);
  1617. if ((lqistate >= 0x1E && lqistate <= 0x24)
  1618. || (lqistate == 0x29)) {
  1619. #ifdef AHD_DEBUG
  1620. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1621. printf("%s: NLQCRC found via LQISTATE\n",
  1622. ahd_name(ahd));
  1623. }
  1624. #endif
  1625. lqistat1 |= LQICRCI_NLQ;
  1626. }
  1627. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1628. }
  1629. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1630. lastphase = ahd_inb(ahd, LASTPHASE);
  1631. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1632. perrdiag = ahd_inb(ahd, PERRDIAG);
  1633. msg_out = MSG_INITIATOR_DET_ERR;
  1634. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
  1635. /*
  1636. * Try to find the SCB associated with this error.
  1637. */
  1638. silent = FALSE;
  1639. if (lqistat1 == 0
  1640. || (lqistat1 & LQICRCI_NLQ) != 0) {
  1641. if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
  1642. ahd_set_active_fifo(ahd);
  1643. scbid = ahd_get_scbptr(ahd);
  1644. scb = ahd_lookup_scb(ahd, scbid);
  1645. if (scb != NULL && SCB_IS_SILENT(scb))
  1646. silent = TRUE;
  1647. }
  1648. cur_col = 0;
  1649. if (silent == FALSE) {
  1650. printf("%s: Transmission error detected\n", ahd_name(ahd));
  1651. ahd_lqistat1_print(lqistat1, &cur_col, 50);
  1652. ahd_lastphase_print(lastphase, &cur_col, 50);
  1653. ahd_scsisigi_print(curphase, &cur_col, 50);
  1654. ahd_perrdiag_print(perrdiag, &cur_col, 50);
  1655. printf("\n");
  1656. ahd_dump_card_state(ahd);
  1657. }
  1658. if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
  1659. if (silent == FALSE) {
  1660. printf("%s: Gross protocol error during incoming "
  1661. "packet. lqistat1 == 0x%x. Resetting bus.\n",
  1662. ahd_name(ahd), lqistat1);
  1663. }
  1664. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1665. return;
  1666. } else if ((lqistat1 & LQICRCI_LQ) != 0) {
  1667. /*
  1668. * A CRC error has been detected on an incoming LQ.
  1669. * The bus is currently hung on the last ACK.
  1670. * Hit LQIRETRY to release the last ack, and
  1671. * wait for the sequencer to determine that ATNO
  1672. * is asserted while in message out to take us
  1673. * to our host message loop. No NONPACKREQ or
  1674. * LQIPHASE type errors will occur in this
  1675. * scenario. After this first LQIRETRY, the LQI
  1676. * manager will be in ISELO where it will
  1677. * happily sit until another packet phase begins.
  1678. * Unexpected bus free detection is enabled
  1679. * through any phases that occur after we release
  1680. * this last ack until the LQI manager sees a
  1681. * packet phase. This implies we may have to
  1682. * ignore a perfectly valid "unexected busfree"
  1683. * after our "initiator detected error" message is
  1684. * sent. A busfree is the expected response after
  1685. * we tell the target that it's L_Q was corrupted.
  1686. * (SPI4R09 10.7.3.3.3)
  1687. */
  1688. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1689. printf("LQIRetry for LQICRCI_LQ to release ACK\n");
  1690. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1691. /*
  1692. * We detected a CRC error in a NON-LQ packet.
  1693. * The hardware has varying behavior in this situation
  1694. * depending on whether this packet was part of a
  1695. * stream or not.
  1696. *
  1697. * PKT by PKT mode:
  1698. * The hardware has already acked the complete packet.
  1699. * If the target honors our outstanding ATN condition,
  1700. * we should be (or soon will be) in MSGOUT phase.
  1701. * This will trigger the LQIPHASE_LQ status bit as the
  1702. * hardware was expecting another LQ. Unexpected
  1703. * busfree detection is enabled. Once LQIPHASE_LQ is
  1704. * true (first entry into host message loop is much
  1705. * the same), we must clear LQIPHASE_LQ and hit
  1706. * LQIRETRY so the hardware is ready to handle
  1707. * a future LQ. NONPACKREQ will not be asserted again
  1708. * once we hit LQIRETRY until another packet is
  1709. * processed. The target may either go busfree
  1710. * or start another packet in response to our message.
  1711. *
  1712. * Read Streaming P0 asserted:
  1713. * If we raise ATN and the target completes the entire
  1714. * stream (P0 asserted during the last packet), the
  1715. * hardware will ack all data and return to the ISTART
  1716. * state. When the target reponds to our ATN condition,
  1717. * LQIPHASE_LQ will be asserted. We should respond to
  1718. * this with an LQIRETRY to prepare for any future
  1719. * packets. NONPACKREQ will not be asserted again
  1720. * once we hit LQIRETRY until another packet is
  1721. * processed. The target may either go busfree or
  1722. * start another packet in response to our message.
  1723. * Busfree detection is enabled.
  1724. *
  1725. * Read Streaming P0 not asserted:
  1726. * If we raise ATN and the target transitions to
  1727. * MSGOUT in or after a packet where P0 is not
  1728. * asserted, the hardware will assert LQIPHASE_NLQ.
  1729. * We should respond to the LQIPHASE_NLQ with an
  1730. * LQIRETRY. Should the target stay in a non-pkt
  1731. * phase after we send our message, the hardware
  1732. * will assert LQIPHASE_LQ. Recovery is then just as
  1733. * listed above for the read streaming with P0 asserted.
  1734. * Busfree detection is enabled.
  1735. */
  1736. if (silent == FALSE)
  1737. printf("LQICRC_NLQ\n");
  1738. if (scb == NULL) {
  1739. printf("%s: No SCB valid for LQICRC_NLQ. "
  1740. "Resetting bus\n", ahd_name(ahd));
  1741. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1742. return;
  1743. }
  1744. } else if ((lqistat1 & LQIBADLQI) != 0) {
  1745. printf("Need to handle BADLQI!\n");
  1746. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1747. return;
  1748. } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
  1749. if ((curphase & ~P_DATAIN_DT) != 0) {
  1750. /* Ack the byte. So we can continue. */
  1751. if (silent == FALSE)
  1752. printf("Acking %s to clear perror\n",
  1753. ahd_lookup_phase_entry(curphase)->phasemsg);
  1754. ahd_inb(ahd, SCSIDAT);
  1755. }
  1756. if (curphase == P_MESGIN)
  1757. msg_out = MSG_PARITY_ERROR;
  1758. }
  1759. /*
  1760. * We've set the hardware to assert ATN if we
  1761. * get a parity error on "in" phases, so all we
  1762. * need to do is stuff the message buffer with
  1763. * the appropriate message. "In" phases have set
  1764. * mesg_out to something other than MSG_NOP.
  1765. */
  1766. ahd->send_msg_perror = msg_out;
  1767. if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
  1768. scb->flags |= SCB_TRANSMISSION_ERROR;
  1769. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1770. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1771. ahd_unpause(ahd);
  1772. }
  1773. static void
  1774. ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
  1775. {
  1776. /*
  1777. * Clear the sources of the interrupts.
  1778. */
  1779. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1780. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1781. /*
  1782. * If the "illegal" phase changes were in response
  1783. * to our ATN to flag a CRC error, AND we ended up
  1784. * on packet boundaries, clear the error, restart the
  1785. * LQI manager as appropriate, and go on our merry
  1786. * way toward sending the message. Otherwise, reset
  1787. * the bus to clear the error.
  1788. */
  1789. ahd_set_active_fifo(ahd);
  1790. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
  1791. && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
  1792. if ((lqistat1 & LQIPHASE_LQ) != 0) {
  1793. printf("LQIRETRY for LQIPHASE_LQ\n");
  1794. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1795. } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
  1796. printf("LQIRETRY for LQIPHASE_NLQ\n");
  1797. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1798. } else
  1799. panic("ahd_handle_lqiphase_error: No phase errors\n");
  1800. ahd_dump_card_state(ahd);
  1801. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1802. ahd_unpause(ahd);
  1803. } else {
  1804. printf("Reseting Channel for LQI Phase error\n");
  1805. ahd_dump_card_state(ahd);
  1806. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1807. }
  1808. }
  1809. /*
  1810. * Packetized unexpected or expected busfree.
  1811. * Entered in mode based on busfreetime.
  1812. */
  1813. static int
  1814. ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
  1815. {
  1816. u_int lqostat1;
  1817. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  1818. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  1819. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1820. if ((lqostat1 & LQOBUSFREE) != 0) {
  1821. struct scb *scb;
  1822. u_int scbid;
  1823. u_int saved_scbptr;
  1824. u_int waiting_h;
  1825. u_int waiting_t;
  1826. u_int next;
  1827. if ((busfreetime & BUSFREE_LQO) == 0)
  1828. printf("%s: Warning, BUSFREE time is 0x%x. "
  1829. "Expected BUSFREE_LQO.\n",
  1830. ahd_name(ahd), busfreetime);
  1831. /*
  1832. * The LQO manager detected an unexpected busfree
  1833. * either:
  1834. *
  1835. * 1) During an outgoing LQ.
  1836. * 2) After an outgoing LQ but before the first
  1837. * REQ of the command packet.
  1838. * 3) During an outgoing command packet.
  1839. *
  1840. * In all cases, CURRSCB is pointing to the
  1841. * SCB that encountered the failure. Clean
  1842. * up the queue, clear SELDO and LQOBUSFREE,
  1843. * and allow the sequencer to restart the select
  1844. * out at its lesure.
  1845. */
  1846. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1847. scbid = ahd_inw(ahd, CURRSCB);
  1848. scb = ahd_lookup_scb(ahd, scbid);
  1849. if (scb == NULL)
  1850. panic("SCB not valid during LQOBUSFREE");
  1851. /*
  1852. * Clear the status.
  1853. */
  1854. ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
  1855. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
  1856. ahd_outb(ahd, CLRLQOINT1, 0);
  1857. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1858. ahd_flush_device_writes(ahd);
  1859. ahd_outb(ahd, CLRSINT0, CLRSELDO);
  1860. /*
  1861. * Return the LQO manager to its idle loop. It will
  1862. * not do this automatically if the busfree occurs
  1863. * after the first REQ of either the LQ or command
  1864. * packet or between the LQ and command packet.
  1865. */
  1866. ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
  1867. /*
  1868. * Update the waiting for selection queue so
  1869. * we restart on the correct SCB.
  1870. */
  1871. waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
  1872. saved_scbptr = ahd_get_scbptr(ahd);
  1873. if (waiting_h != scbid) {
  1874. ahd_outw(ahd, WAITING_TID_HEAD, scbid);
  1875. waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
  1876. if (waiting_t == waiting_h) {
  1877. ahd_outw(ahd, WAITING_TID_TAIL, scbid);
  1878. next = SCB_LIST_NULL;
  1879. } else {
  1880. ahd_set_scbptr(ahd, waiting_h);
  1881. next = ahd_inw_scbram(ahd, SCB_NEXT2);
  1882. }
  1883. ahd_set_scbptr(ahd, scbid);
  1884. ahd_outw(ahd, SCB_NEXT2, next);
  1885. }
  1886. ahd_set_scbptr(ahd, saved_scbptr);
  1887. if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
  1888. if (SCB_IS_SILENT(scb) == FALSE) {
  1889. ahd_print_path(ahd, scb);
  1890. printf("Probable outgoing LQ CRC error. "
  1891. "Retrying command\n");
  1892. }
  1893. scb->crc_retry_count++;
  1894. } else {
  1895. ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1896. ahd_freeze_scb(scb);
  1897. ahd_freeze_devq(ahd, scb);
  1898. }
  1899. /* Return unpausing the sequencer. */
  1900. return (0);
  1901. } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
  1902. /*
  1903. * Ignore what are really parity errors that
  1904. * occur on the last REQ of a free running
  1905. * clock prior to going busfree. Some drives
  1906. * do not properly active negate just before
  1907. * going busfree resulting in a parity glitch.
  1908. */
  1909. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
  1910. #ifdef AHD_DEBUG
  1911. if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
  1912. printf("%s: Parity on last REQ detected "
  1913. "during busfree phase.\n",
  1914. ahd_name(ahd));
  1915. #endif
  1916. /* Return unpausing the sequencer. */
  1917. return (0);
  1918. }
  1919. if (ahd->src_mode != AHD_MODE_SCSI) {
  1920. u_int scbid;
  1921. struct scb *scb;
  1922. scbid = ahd_get_scbptr(ahd);
  1923. scb = ahd_lookup_scb(ahd, scbid);
  1924. ahd_print_path(ahd, scb);
  1925. printf("Unexpected PKT busfree condition\n");
  1926. ahd_dump_card_state(ahd);
  1927. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
  1928. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1929. ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
  1930. /* Return restarting the sequencer. */
  1931. return (1);
  1932. }
  1933. printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
  1934. ahd_dump_card_state(ahd);
  1935. /* Restart the sequencer. */
  1936. return (1);
  1937. }
  1938. /*
  1939. * Non-packetized unexpected or expected busfree.
  1940. */
  1941. static int
  1942. ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
  1943. {
  1944. struct ahd_devinfo devinfo;
  1945. struct scb *scb;
  1946. u_int lastphase;
  1947. u_int saved_scsiid;
  1948. u_int saved_lun;
  1949. u_int target;
  1950. u_int initiator_role_id;
  1951. u_int scbid;
  1952. u_int ppr_busfree;
  1953. int printerror;
  1954. /*
  1955. * Look at what phase we were last in. If its message out,
  1956. * chances are pretty good that the busfree was in response
  1957. * to one of our abort requests.
  1958. */
  1959. lastphase = ahd_inb(ahd, LASTPHASE);
  1960. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  1961. saved_lun = ahd_inb(ahd, SAVED_LUN);
  1962. target = SCSIID_TARGET(ahd, saved_scsiid);
  1963. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  1964. ahd_compile_devinfo(&devinfo, initiator_role_id,
  1965. target, saved_lun, 'A', ROLE_INITIATOR);
  1966. printerror = 1;
  1967. scbid = ahd_get_scbptr(ahd);
  1968. scb = ahd_lookup_scb(ahd, scbid);
  1969. if (scb != NULL
  1970. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  1971. scb = NULL;
  1972. ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
  1973. if (lastphase == P_MESGOUT) {
  1974. u_int tag;
  1975. tag = SCB_LIST_NULL;
  1976. if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
  1977. || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
  1978. int found;
  1979. int sent_msg;
  1980. if (scb == NULL) {
  1981. ahd_print_devinfo(ahd, &devinfo);
  1982. printf("Abort for unidentified "
  1983. "connection completed.\n");
  1984. /* restart the sequencer. */
  1985. return (1);
  1986. }
  1987. sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
  1988. ahd_print_path(ahd, scb);
  1989. printf("SCB %d - Abort%s Completed.\n",
  1990. SCB_GET_TAG(scb),
  1991. sent_msg == MSG_ABORT_TAG ? "" : " Tag");
  1992. if (sent_msg == MSG_ABORT_TAG)
  1993. tag = SCB_GET_TAG(scb);
  1994. if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
  1995. /*
  1996. * This abort is in response to an
  1997. * unexpected switch to command phase
  1998. * for a packetized connection. Since
  1999. * the identify message was never sent,
  2000. * "saved lun" is 0. We really want to
  2001. * abort only the SCB that encountered
  2002. * this error, which could have a different
  2003. * lun. The SCB will be retried so the OS
  2004. * will see the UA after renegotiating to
  2005. * packetized.
  2006. */
  2007. tag = SCB_GET_TAG(scb);
  2008. saved_lun = scb->hscb->lun;
  2009. }
  2010. found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
  2011. tag, ROLE_INITIATOR,
  2012. CAM_REQ_ABORTED);
  2013. printf("found == 0x%x\n", found);
  2014. printerror = 0;
  2015. } else if (ahd_sent_msg(ahd, AHDMSG_1B,
  2016. MSG_BUS_DEV_RESET, TRUE)) {
  2017. #ifdef __FreeBSD__
  2018. /*
  2019. * Don't mark the user's request for this BDR
  2020. * as completing with CAM_BDR_SENT. CAM3
  2021. * specifies CAM_REQ_CMP.
  2022. */
  2023. if (scb != NULL
  2024. && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  2025. && ahd_match_scb(ahd, scb, target, 'A',
  2026. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  2027. ROLE_INITIATOR))
  2028. ahd_set_transaction_status(scb, CAM_REQ_CMP);
  2029. #endif
  2030. ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
  2031. CAM_BDR_SENT, "Bus Device Reset",
  2032. /*verbose_level*/0);
  2033. printerror = 0;
  2034. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
  2035. && ppr_busfree == 0) {
  2036. struct ahd_initiator_tinfo *tinfo;
  2037. struct ahd_tmode_tstate *tstate;
  2038. /*
  2039. * PPR Rejected. Try non-ppr negotiation
  2040. * and retry command.
  2041. */
  2042. #ifdef AHD_DEBUG
  2043. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2044. printf("PPR negotiation rejected busfree.\n");
  2045. #endif
  2046. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  2047. devinfo.our_scsiid,
  2048. devinfo.target, &tstate);
  2049. tinfo->curr.transport_version = 2;
  2050. tinfo->goal.transport_version = 2;
  2051. tinfo->goal.ppr_options = 0;
  2052. ahd_qinfifo_requeue_tail(ahd, scb);
  2053. printerror = 0;
  2054. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
  2055. && ppr_busfree == 0) {
  2056. /*
  2057. * Negotiation Rejected. Go-narrow and
  2058. * retry command.
  2059. */
  2060. #ifdef AHD_DEBUG
  2061. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2062. printf("WDTR negotiation rejected busfree.\n");
  2063. #endif
  2064. ahd_set_width(ahd, &devinfo,
  2065. MSG_EXT_WDTR_BUS_8_BIT,
  2066. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2067. /*paused*/TRUE);
  2068. ahd_qinfifo_requeue_tail(ahd, scb);
  2069. printerror = 0;
  2070. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
  2071. && ppr_busfree == 0) {
  2072. /*
  2073. * Negotiation Rejected. Go-async and
  2074. * retry command.
  2075. */
  2076. #ifdef AHD_DEBUG
  2077. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2078. printf("SDTR negotiation rejected busfree.\n");
  2079. #endif
  2080. ahd_set_syncrate(ahd, &devinfo,
  2081. /*period*/0, /*offset*/0,
  2082. /*ppr_options*/0,
  2083. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2084. /*paused*/TRUE);
  2085. ahd_qinfifo_requeue_tail(ahd, scb);
  2086. printerror = 0;
  2087. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
  2088. && ahd_sent_msg(ahd, AHDMSG_1B,
  2089. MSG_INITIATOR_DET_ERR, TRUE)) {
  2090. #ifdef AHD_DEBUG
  2091. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2092. printf("Expected IDE Busfree\n");
  2093. #endif
  2094. printerror = 0;
  2095. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
  2096. && ahd_sent_msg(ahd, AHDMSG_1B,
  2097. MSG_MESSAGE_REJECT, TRUE)) {
  2098. #ifdef AHD_DEBUG
  2099. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2100. printf("Expected QAS Reject Busfree\n");
  2101. #endif
  2102. printerror = 0;
  2103. }
  2104. }
  2105. /*
  2106. * The busfree required flag is honored at the end of
  2107. * the message phases. We check it last in case we
  2108. * had to send some other message that caused a busfree.
  2109. */
  2110. if (printerror != 0
  2111. && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
  2112. && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
  2113. ahd_freeze_devq(ahd, scb);
  2114. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  2115. ahd_freeze_scb(scb);
  2116. if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
  2117. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  2118. SCB_GET_CHANNEL(ahd, scb),
  2119. SCB_GET_LUN(scb), SCB_LIST_NULL,
  2120. ROLE_INITIATOR, CAM_REQ_ABORTED);
  2121. } else {
  2122. #ifdef AHD_DEBUG
  2123. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2124. printf("PPR Negotiation Busfree.\n");
  2125. #endif
  2126. ahd_done(ahd, scb);
  2127. }
  2128. printerror = 0;
  2129. }
  2130. if (printerror != 0) {
  2131. int aborted;
  2132. aborted = 0;
  2133. if (scb != NULL) {
  2134. u_int tag;
  2135. if ((scb->hscb->control & TAG_ENB) != 0)
  2136. tag = SCB_GET_TAG(scb);
  2137. else
  2138. tag = SCB_LIST_NULL;
  2139. ahd_print_path(ahd, scb);
  2140. aborted = ahd_abort_scbs(ahd, target, 'A',
  2141. SCB_GET_LUN(scb), tag,
  2142. ROLE_INITIATOR,
  2143. CAM_UNEXP_BUSFREE);
  2144. } else {
  2145. /*
  2146. * We had not fully identified this connection,
  2147. * so we cannot abort anything.
  2148. */
  2149. printf("%s: ", ahd_name(ahd));
  2150. }
  2151. if (lastphase != P_BUSFREE)
  2152. ahd_force_renegotiation(ahd, &devinfo);
  2153. printf("Unexpected busfree %s, %d SCBs aborted, "
  2154. "PRGMCNT == 0x%x\n",
  2155. ahd_lookup_phase_entry(lastphase)->phasemsg,
  2156. aborted,
  2157. ahd_inb(ahd, PRGMCNT)
  2158. | (ahd_inb(ahd, PRGMCNT+1) << 8));
  2159. ahd_dump_card_state(ahd);
  2160. }
  2161. /* Always restart the sequencer. */
  2162. return (1);
  2163. }
  2164. static void
  2165. ahd_handle_proto_violation(struct ahd_softc *ahd)
  2166. {
  2167. struct ahd_devinfo devinfo;
  2168. struct scb *scb;
  2169. u_int scbid;
  2170. u_int seq_flags;
  2171. u_int curphase;
  2172. u_int lastphase;
  2173. int found;
  2174. ahd_fetch_devinfo(ahd, &devinfo);
  2175. scbid = ahd_get_scbptr(ahd);
  2176. scb = ahd_lookup_scb(ahd, scbid);
  2177. seq_flags = ahd_inb(ahd, SEQ_FLAGS);
  2178. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  2179. lastphase = ahd_inb(ahd, LASTPHASE);
  2180. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2181. /*
  2182. * The reconnecting target either did not send an
  2183. * identify message, or did, but we didn't find an SCB
  2184. * to match.
  2185. */
  2186. ahd_print_devinfo(ahd, &devinfo);
  2187. printf("Target did not send an IDENTIFY message. "
  2188. "LASTPHASE = 0x%x.\n", lastphase);
  2189. scb = NULL;
  2190. } else if (scb == NULL) {
  2191. /*
  2192. * We don't seem to have an SCB active for this
  2193. * transaction. Print an error and reset the bus.
  2194. */
  2195. ahd_print_devinfo(ahd, &devinfo);
  2196. printf("No SCB found during protocol violation\n");
  2197. goto proto_violation_reset;
  2198. } else {
  2199. ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2200. if ((seq_flags & NO_CDB_SENT) != 0) {
  2201. ahd_print_path(ahd, scb);
  2202. printf("No or incomplete CDB sent to device.\n");
  2203. } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
  2204. & STATUS_RCVD) == 0) {
  2205. /*
  2206. * The target never bothered to provide status to
  2207. * us prior to completing the command. Since we don't
  2208. * know the disposition of this command, we must attempt
  2209. * to abort it. Assert ATN and prepare to send an abort
  2210. * message.
  2211. */
  2212. ahd_print_path(ahd, scb);
  2213. printf("Completed command without status.\n");
  2214. } else {
  2215. ahd_print_path(ahd, scb);
  2216. printf("Unknown protocol violation.\n");
  2217. ahd_dump_card_state(ahd);
  2218. }
  2219. }
  2220. if ((lastphase & ~P_DATAIN_DT) == 0
  2221. || lastphase == P_COMMAND) {
  2222. proto_violation_reset:
  2223. /*
  2224. * Target either went directly to data
  2225. * phase or didn't respond to our ATN.
  2226. * The only safe thing to do is to blow
  2227. * it away with a bus reset.
  2228. */
  2229. found = ahd_reset_channel(ahd, 'A', TRUE);
  2230. printf("%s: Issued Channel %c Bus Reset. "
  2231. "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
  2232. } else {
  2233. /*
  2234. * Leave the selection hardware off in case
  2235. * this abort attempt will affect yet to
  2236. * be sent commands.
  2237. */
  2238. ahd_outb(ahd, SCSISEQ0,
  2239. ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  2240. ahd_assert_atn(ahd);
  2241. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  2242. if (scb == NULL) {
  2243. ahd_print_devinfo(ahd, &devinfo);
  2244. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  2245. ahd->msgout_len = 1;
  2246. ahd->msgout_index = 0;
  2247. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2248. } else {
  2249. ahd_print_path(ahd, scb);
  2250. scb->flags |= SCB_ABORT;
  2251. }
  2252. printf("Protocol violation %s. Attempting to abort.\n",
  2253. ahd_lookup_phase_entry(curphase)->phasemsg);
  2254. }
  2255. }
  2256. /*
  2257. * Force renegotiation to occur the next time we initiate
  2258. * a command to the current device.
  2259. */
  2260. static void
  2261. ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  2262. {
  2263. struct ahd_initiator_tinfo *targ_info;
  2264. struct ahd_tmode_tstate *tstate;
  2265. #ifdef AHD_DEBUG
  2266. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2267. ahd_print_devinfo(ahd, devinfo);
  2268. printf("Forcing renegotiation\n");
  2269. }
  2270. #endif
  2271. targ_info = ahd_fetch_transinfo(ahd,
  2272. devinfo->channel,
  2273. devinfo->our_scsiid,
  2274. devinfo->target,
  2275. &tstate);
  2276. ahd_update_neg_request(ahd, devinfo, tstate,
  2277. targ_info, AHD_NEG_IF_NON_ASYNC);
  2278. }
  2279. #define AHD_MAX_STEPS 2000
  2280. void
  2281. ahd_clear_critical_section(struct ahd_softc *ahd)
  2282. {
  2283. ahd_mode_state saved_modes;
  2284. int stepping;
  2285. int steps;
  2286. int first_instr;
  2287. u_int simode0;
  2288. u_int simode1;
  2289. u_int simode3;
  2290. u_int lqimode0;
  2291. u_int lqimode1;
  2292. u_int lqomode0;
  2293. u_int lqomode1;
  2294. if (ahd->num_critical_sections == 0)
  2295. return;
  2296. stepping = FALSE;
  2297. steps = 0;
  2298. first_instr = 0;
  2299. simode0 = 0;
  2300. simode1 = 0;
  2301. simode3 = 0;
  2302. lqimode0 = 0;
  2303. lqimode1 = 0;
  2304. lqomode0 = 0;
  2305. lqomode1 = 0;
  2306. saved_modes = ahd_save_modes(ahd);
  2307. for (;;) {
  2308. struct cs *cs;
  2309. u_int seqaddr;
  2310. u_int i;
  2311. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2312. seqaddr = ahd_inb(ahd, CURADDR)
  2313. | (ahd_inb(ahd, CURADDR+1) << 8);
  2314. cs = ahd->critical_sections;
  2315. for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
  2316. if (cs->begin < seqaddr && cs->end >= seqaddr)
  2317. break;
  2318. }
  2319. if (i == ahd->num_critical_sections)
  2320. break;
  2321. if (steps > AHD_MAX_STEPS) {
  2322. printf("%s: Infinite loop in critical section\n"
  2323. "%s: First Instruction 0x%x now 0x%x\n",
  2324. ahd_name(ahd), ahd_name(ahd), first_instr,
  2325. seqaddr);
  2326. ahd_dump_card_state(ahd);
  2327. panic("critical section loop");
  2328. }
  2329. steps++;
  2330. #ifdef AHD_DEBUG
  2331. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  2332. printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
  2333. seqaddr);
  2334. #endif
  2335. if (stepping == FALSE) {
  2336. first_instr = seqaddr;
  2337. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2338. simode0 = ahd_inb(ahd, SIMODE0);
  2339. simode3 = ahd_inb(ahd, SIMODE3);
  2340. lqimode0 = ahd_inb(ahd, LQIMODE0);
  2341. lqimode1 = ahd_inb(ahd, LQIMODE1);
  2342. lqomode0 = ahd_inb(ahd, LQOMODE0);
  2343. lqomode1 = ahd_inb(ahd, LQOMODE1);
  2344. ahd_outb(ahd, SIMODE0, 0);
  2345. ahd_outb(ahd, SIMODE3, 0);
  2346. ahd_outb(ahd, LQIMODE0, 0);
  2347. ahd_outb(ahd, LQIMODE1, 0);
  2348. ahd_outb(ahd, LQOMODE0, 0);
  2349. ahd_outb(ahd, LQOMODE1, 0);
  2350. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2351. simode1 = ahd_inb(ahd, SIMODE1);
  2352. /*
  2353. * We don't clear ENBUSFREE. Unfortunately
  2354. * we cannot re-enable busfree detection within
  2355. * the current connection, so we must leave it
  2356. * on while single stepping.
  2357. */
  2358. ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
  2359. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
  2360. stepping = TRUE;
  2361. }
  2362. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  2363. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2364. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  2365. ahd_outb(ahd, HCNTRL, ahd->unpause);
  2366. while (!ahd_is_paused(ahd))
  2367. ahd_delay(200);
  2368. ahd_update_modes(ahd);
  2369. }
  2370. if (stepping) {
  2371. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2372. ahd_outb(ahd, SIMODE0, simode0);
  2373. ahd_outb(ahd, SIMODE3, simode3);
  2374. ahd_outb(ahd, LQIMODE0, lqimode0);
  2375. ahd_outb(ahd, LQIMODE1, lqimode1);
  2376. ahd_outb(ahd, LQOMODE0, lqomode0);
  2377. ahd_outb(ahd, LQOMODE1, lqomode1);
  2378. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2379. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
  2380. ahd_outb(ahd, SIMODE1, simode1);
  2381. /*
  2382. * SCSIINT seems to glitch occassionally when
  2383. * the interrupt masks are restored. Clear SCSIINT
  2384. * one more time so that only persistent errors
  2385. * are seen as a real interrupt.
  2386. */
  2387. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2388. }
  2389. ahd_restore_modes(ahd, saved_modes);
  2390. }
  2391. /*
  2392. * Clear any pending interrupt status.
  2393. */
  2394. void
  2395. ahd_clear_intstat(struct ahd_softc *ahd)
  2396. {
  2397. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  2398. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  2399. /* Clear any interrupt conditions this may have caused */
  2400. ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
  2401. |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
  2402. ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
  2403. |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
  2404. |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
  2405. ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
  2406. |CLRLQOATNPKT|CLRLQOTCRC);
  2407. ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
  2408. |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
  2409. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  2410. ahd_outb(ahd, CLRLQOINT0, 0);
  2411. ahd_outb(ahd, CLRLQOINT1, 0);
  2412. }
  2413. ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
  2414. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  2415. |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
  2416. ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
  2417. |CLRIOERR|CLROVERRUN);
  2418. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2419. }
  2420. /**************************** Debugging Routines ******************************/
  2421. #ifdef AHD_DEBUG
  2422. uint32_t ahd_debug = AHD_DEBUG_OPTS;
  2423. #endif
  2424. void
  2425. ahd_print_scb(struct scb *scb)
  2426. {
  2427. struct hardware_scb *hscb;
  2428. int i;
  2429. hscb = scb->hscb;
  2430. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
  2431. (void *)scb,
  2432. hscb->control,
  2433. hscb->scsiid,
  2434. hscb->lun,
  2435. hscb->cdb_len);
  2436. printf("Shared Data: ");
  2437. for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
  2438. printf("%#02x", hscb->shared_data.idata.cdb[i]);
  2439. printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
  2440. (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
  2441. (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
  2442. ahd_le32toh(hscb->datacnt),
  2443. ahd_le32toh(hscb->sgptr),
  2444. SCB_GET_TAG(scb));
  2445. ahd_dump_sglist(scb);
  2446. }
  2447. void
  2448. ahd_dump_sglist(struct scb *scb)
  2449. {
  2450. int i;
  2451. if (scb->sg_count > 0) {
  2452. if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
  2453. struct ahd_dma64_seg *sg_list;
  2454. sg_list = (struct ahd_dma64_seg*)scb->sg_list;
  2455. for (i = 0; i < scb->sg_count; i++) {
  2456. uint64_t addr;
  2457. uint32_t len;
  2458. addr = ahd_le64toh(sg_list[i].addr);
  2459. len = ahd_le32toh(sg_list[i].len);
  2460. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2461. i,
  2462. (uint32_t)((addr >> 32) & 0xFFFFFFFF),
  2463. (uint32_t)(addr & 0xFFFFFFFF),
  2464. sg_list[i].len & AHD_SG_LEN_MASK,
  2465. (sg_list[i].len & AHD_DMA_LAST_SEG)
  2466. ? " Last" : "");
  2467. }
  2468. } else {
  2469. struct ahd_dma_seg *sg_list;
  2470. sg_list = (struct ahd_dma_seg*)scb->sg_list;
  2471. for (i = 0; i < scb->sg_count; i++) {
  2472. uint32_t len;
  2473. len = ahd_le32toh(sg_list[i].len);
  2474. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2475. i,
  2476. (len & AHD_SG_HIGH_ADDR_MASK) >> 24,
  2477. ahd_le32toh(sg_list[i].addr),
  2478. len & AHD_SG_LEN_MASK,
  2479. len & AHD_DMA_LAST_SEG ? " Last" : "");
  2480. }
  2481. }
  2482. }
  2483. }
  2484. /************************* Transfer Negotiation *******************************/
  2485. /*
  2486. * Allocate per target mode instance (ID we respond to as a target)
  2487. * transfer negotiation data structures.
  2488. */
  2489. static struct ahd_tmode_tstate *
  2490. ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
  2491. {
  2492. struct ahd_tmode_tstate *master_tstate;
  2493. struct ahd_tmode_tstate *tstate;
  2494. int i;
  2495. master_tstate = ahd->enabled_targets[ahd->our_id];
  2496. if (ahd->enabled_targets[scsi_id] != NULL
  2497. && ahd->enabled_targets[scsi_id] != master_tstate)
  2498. panic("%s: ahd_alloc_tstate - Target already allocated",
  2499. ahd_name(ahd));
  2500. tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
  2501. if (tstate == NULL)
  2502. return (NULL);
  2503. /*
  2504. * If we have allocated a master tstate, copy user settings from
  2505. * the master tstate (taken from SRAM or the EEPROM) for this
  2506. * channel, but reset our current and goal settings to async/narrow
  2507. * until an initiator talks to us.
  2508. */
  2509. if (master_tstate != NULL) {
  2510. memcpy(tstate, master_tstate, sizeof(*tstate));
  2511. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  2512. for (i = 0; i < 16; i++) {
  2513. memset(&tstate->transinfo[i].curr, 0,
  2514. sizeof(tstate->transinfo[i].curr));
  2515. memset(&tstate->transinfo[i].goal, 0,
  2516. sizeof(tstate->transinfo[i].goal));
  2517. }
  2518. } else
  2519. memset(tstate, 0, sizeof(*tstate));
  2520. ahd->enabled_targets[scsi_id] = tstate;
  2521. return (tstate);
  2522. }
  2523. #ifdef AHD_TARGET_MODE
  2524. /*
  2525. * Free per target mode instance (ID we respond to as a target)
  2526. * transfer negotiation data structures.
  2527. */
  2528. static void
  2529. ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
  2530. {
  2531. struct ahd_tmode_tstate *tstate;
  2532. /*
  2533. * Don't clean up our "master" tstate.
  2534. * It has our default user settings.
  2535. */
  2536. if (scsi_id == ahd->our_id
  2537. && force == FALSE)
  2538. return;
  2539. tstate = ahd->enabled_targets[scsi_id];
  2540. if (tstate != NULL)
  2541. free(tstate, M_DEVBUF);
  2542. ahd->enabled_targets[scsi_id] = NULL;
  2543. }
  2544. #endif
  2545. /*
  2546. * Called when we have an active connection to a target on the bus,
  2547. * this function finds the nearest period to the input period limited
  2548. * by the capabilities of the bus connectivity of and sync settings for
  2549. * the target.
  2550. */
  2551. void
  2552. ahd_devlimited_syncrate(struct ahd_softc *ahd,
  2553. struct ahd_initiator_tinfo *tinfo,
  2554. u_int *period, u_int *ppr_options, role_t role)
  2555. {
  2556. struct ahd_transinfo *transinfo;
  2557. u_int maxsync;
  2558. if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
  2559. && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
  2560. maxsync = AHD_SYNCRATE_PACED;
  2561. } else {
  2562. maxsync = AHD_SYNCRATE_ULTRA;
  2563. /* Can't do DT related options on an SE bus */
  2564. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2565. }
  2566. /*
  2567. * Never allow a value higher than our current goal
  2568. * period otherwise we may allow a target initiated
  2569. * negotiation to go above the limit as set by the
  2570. * user. In the case of an initiator initiated
  2571. * sync negotiation, we limit based on the user
  2572. * setting. This allows the system to still accept
  2573. * incoming negotiations even if target initiated
  2574. * negotiation is not performed.
  2575. */
  2576. if (role == ROLE_TARGET)
  2577. transinfo = &tinfo->user;
  2578. else
  2579. transinfo = &tinfo->goal;
  2580. *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
  2581. if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
  2582. maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
  2583. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2584. }
  2585. if (transinfo->period == 0) {
  2586. *period = 0;
  2587. *ppr_options = 0;
  2588. } else {
  2589. *period = MAX(*period, transinfo->period);
  2590. ahd_find_syncrate(ahd, period, ppr_options, maxsync);
  2591. }
  2592. }
  2593. /*
  2594. * Look up the valid period to SCSIRATE conversion in our table.
  2595. * Return the period and offset that should be sent to the target
  2596. * if this was the beginning of an SDTR.
  2597. */
  2598. void
  2599. ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
  2600. u_int *ppr_options, u_int maxsync)
  2601. {
  2602. if (*period < maxsync)
  2603. *period = maxsync;
  2604. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
  2605. && *period > AHD_SYNCRATE_MIN_DT)
  2606. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2607. if (*period > AHD_SYNCRATE_MIN)
  2608. *period = 0;
  2609. /* Honor PPR option conformance rules. */
  2610. if (*period > AHD_SYNCRATE_PACED)
  2611. *ppr_options &= ~MSG_EXT_PPR_RTI;
  2612. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  2613. *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
  2614. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
  2615. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2616. /* Skip all PACED only entries if IU is not available */
  2617. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
  2618. && *period < AHD_SYNCRATE_DT)
  2619. *period = AHD_SYNCRATE_DT;
  2620. /* Skip all DT only entries if DT is not available */
  2621. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  2622. && *period < AHD_SYNCRATE_ULTRA2)
  2623. *period = AHD_SYNCRATE_ULTRA2;
  2624. }
  2625. /*
  2626. * Truncate the given synchronous offset to a value the
  2627. * current adapter type and syncrate are capable of.
  2628. */
  2629. void
  2630. ahd_validate_offset(struct ahd_softc *ahd,
  2631. struct ahd_initiator_tinfo *tinfo,
  2632. u_int period, u_int *offset, int wide,
  2633. role_t role)
  2634. {
  2635. u_int maxoffset;
  2636. /* Limit offset to what we can do */
  2637. if (period == 0)
  2638. maxoffset = 0;
  2639. else if (period <= AHD_SYNCRATE_PACED) {
  2640. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
  2641. maxoffset = MAX_OFFSET_PACED_BUG;
  2642. else
  2643. maxoffset = MAX_OFFSET_PACED;
  2644. } else
  2645. maxoffset = MAX_OFFSET_NON_PACED;
  2646. *offset = MIN(*offset, maxoffset);
  2647. if (tinfo != NULL) {
  2648. if (role == ROLE_TARGET)
  2649. *offset = MIN(*offset, tinfo->user.offset);
  2650. else
  2651. *offset = MIN(*offset, tinfo->goal.offset);
  2652. }
  2653. }
  2654. /*
  2655. * Truncate the given transfer width parameter to a value the
  2656. * current adapter type is capable of.
  2657. */
  2658. void
  2659. ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
  2660. u_int *bus_width, role_t role)
  2661. {
  2662. switch (*bus_width) {
  2663. default:
  2664. if (ahd->features & AHD_WIDE) {
  2665. /* Respond Wide */
  2666. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  2667. break;
  2668. }
  2669. /* FALLTHROUGH */
  2670. case MSG_EXT_WDTR_BUS_8_BIT:
  2671. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  2672. break;
  2673. }
  2674. if (tinfo != NULL) {
  2675. if (role == ROLE_TARGET)
  2676. *bus_width = MIN(tinfo->user.width, *bus_width);
  2677. else
  2678. *bus_width = MIN(tinfo->goal.width, *bus_width);
  2679. }
  2680. }
  2681. /*
  2682. * Update the bitmask of targets for which the controller should
  2683. * negotiate with at the next convenient oportunity. This currently
  2684. * means the next time we send the initial identify messages for
  2685. * a new transaction.
  2686. */
  2687. int
  2688. ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2689. struct ahd_tmode_tstate *tstate,
  2690. struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
  2691. {
  2692. u_int auto_negotiate_orig;
  2693. auto_negotiate_orig = tstate->auto_negotiate;
  2694. if (neg_type == AHD_NEG_ALWAYS) {
  2695. /*
  2696. * Force our "current" settings to be
  2697. * unknown so that unless a bus reset
  2698. * occurs the need to renegotiate is
  2699. * recorded persistently.
  2700. */
  2701. if ((ahd->features & AHD_WIDE) != 0)
  2702. tinfo->curr.width = AHD_WIDTH_UNKNOWN;
  2703. tinfo->curr.period = AHD_PERIOD_UNKNOWN;
  2704. tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
  2705. }
  2706. if (tinfo->curr.period != tinfo->goal.period
  2707. || tinfo->curr.width != tinfo->goal.width
  2708. || tinfo->curr.offset != tinfo->goal.offset
  2709. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  2710. || (neg_type == AHD_NEG_IF_NON_ASYNC
  2711. && (tinfo->goal.offset != 0
  2712. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  2713. || tinfo->goal.ppr_options != 0)))
  2714. tstate->auto_negotiate |= devinfo->target_mask;
  2715. else
  2716. tstate->auto_negotiate &= ~devinfo->target_mask;
  2717. return (auto_negotiate_orig != tstate->auto_negotiate);
  2718. }
  2719. /*
  2720. * Update the user/goal/curr tables of synchronous negotiation
  2721. * parameters as well as, in the case of a current or active update,
  2722. * any data structures on the host controller. In the case of an
  2723. * active update, the specified target is currently talking to us on
  2724. * the bus, so the transfer parameter update must take effect
  2725. * immediately.
  2726. */
  2727. void
  2728. ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2729. u_int period, u_int offset, u_int ppr_options,
  2730. u_int type, int paused)
  2731. {
  2732. struct ahd_initiator_tinfo *tinfo;
  2733. struct ahd_tmode_tstate *tstate;
  2734. u_int old_period;
  2735. u_int old_offset;
  2736. u_int old_ppr;
  2737. int active;
  2738. int update_needed;
  2739. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2740. update_needed = 0;
  2741. if (period == 0 || offset == 0) {
  2742. period = 0;
  2743. offset = 0;
  2744. }
  2745. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2746. devinfo->target, &tstate);
  2747. if ((type & AHD_TRANS_USER) != 0) {
  2748. tinfo->user.period = period;
  2749. tinfo->user.offset = offset;
  2750. tinfo->user.ppr_options = ppr_options;
  2751. }
  2752. if ((type & AHD_TRANS_GOAL) != 0) {
  2753. tinfo->goal.period = period;
  2754. tinfo->goal.offset = offset;
  2755. tinfo->goal.ppr_options = ppr_options;
  2756. }
  2757. old_period = tinfo->curr.period;
  2758. old_offset = tinfo->curr.offset;
  2759. old_ppr = tinfo->curr.ppr_options;
  2760. if ((type & AHD_TRANS_CUR) != 0
  2761. && (old_period != period
  2762. || old_offset != offset
  2763. || old_ppr != ppr_options)) {
  2764. update_needed++;
  2765. tinfo->curr.period = period;
  2766. tinfo->curr.offset = offset;
  2767. tinfo->curr.ppr_options = ppr_options;
  2768. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2769. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2770. if (bootverbose) {
  2771. if (offset != 0) {
  2772. int options;
  2773. printf("%s: target %d synchronous with "
  2774. "period = 0x%x, offset = 0x%x",
  2775. ahd_name(ahd), devinfo->target,
  2776. period, offset);
  2777. options = 0;
  2778. if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
  2779. printf("(RDSTRM");
  2780. options++;
  2781. }
  2782. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
  2783. printf("%s", options ? "|DT" : "(DT");
  2784. options++;
  2785. }
  2786. if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
  2787. printf("%s", options ? "|IU" : "(IU");
  2788. options++;
  2789. }
  2790. if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
  2791. printf("%s", options ? "|RTI" : "(RTI");
  2792. options++;
  2793. }
  2794. if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
  2795. printf("%s", options ? "|QAS" : "(QAS");
  2796. options++;
  2797. }
  2798. if (options != 0)
  2799. printf(")\n");
  2800. else
  2801. printf("\n");
  2802. } else {
  2803. printf("%s: target %d using "
  2804. "asynchronous transfers%s\n",
  2805. ahd_name(ahd), devinfo->target,
  2806. (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
  2807. ? "(QAS)" : "");
  2808. }
  2809. }
  2810. }
  2811. /*
  2812. * Always refresh the neg-table to handle the case of the
  2813. * sequencer setting the ENATNO bit for a MK_MESSAGE request.
  2814. * We will always renegotiate in that case if this is a
  2815. * packetized request. Also manage the busfree expected flag
  2816. * from this common routine so that we catch changes due to
  2817. * WDTR or SDTR messages.
  2818. */
  2819. if ((type & AHD_TRANS_CUR) != 0) {
  2820. if (!paused)
  2821. ahd_pause(ahd);
  2822. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2823. if (!paused)
  2824. ahd_unpause(ahd);
  2825. if (ahd->msg_type != MSG_TYPE_NONE) {
  2826. if ((old_ppr & MSG_EXT_PPR_IU_REQ)
  2827. != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
  2828. #ifdef AHD_DEBUG
  2829. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2830. ahd_print_devinfo(ahd, devinfo);
  2831. printf("Expecting IU Change busfree\n");
  2832. }
  2833. #endif
  2834. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  2835. | MSG_FLAG_IU_REQ_CHANGED;
  2836. }
  2837. if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
  2838. #ifdef AHD_DEBUG
  2839. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2840. printf("PPR with IU_REQ outstanding\n");
  2841. #endif
  2842. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
  2843. }
  2844. }
  2845. }
  2846. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2847. tinfo, AHD_NEG_TO_GOAL);
  2848. if (update_needed && active)
  2849. ahd_update_pending_scbs(ahd);
  2850. }
  2851. /*
  2852. * Update the user/goal/curr tables of wide negotiation
  2853. * parameters as well as, in the case of a current or active update,
  2854. * any data structures on the host controller. In the case of an
  2855. * active update, the specified target is currently talking to us on
  2856. * the bus, so the transfer parameter update must take effect
  2857. * immediately.
  2858. */
  2859. void
  2860. ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2861. u_int width, u_int type, int paused)
  2862. {
  2863. struct ahd_initiator_tinfo *tinfo;
  2864. struct ahd_tmode_tstate *tstate;
  2865. u_int oldwidth;
  2866. int active;
  2867. int update_needed;
  2868. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2869. update_needed = 0;
  2870. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2871. devinfo->target, &tstate);
  2872. if ((type & AHD_TRANS_USER) != 0)
  2873. tinfo->user.width = width;
  2874. if ((type & AHD_TRANS_GOAL) != 0)
  2875. tinfo->goal.width = width;
  2876. oldwidth = tinfo->curr.width;
  2877. if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
  2878. update_needed++;
  2879. tinfo->curr.width = width;
  2880. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2881. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2882. if (bootverbose) {
  2883. printf("%s: target %d using %dbit transfers\n",
  2884. ahd_name(ahd), devinfo->target,
  2885. 8 * (0x01 << width));
  2886. }
  2887. }
  2888. if ((type & AHD_TRANS_CUR) != 0) {
  2889. if (!paused)
  2890. ahd_pause(ahd);
  2891. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2892. if (!paused)
  2893. ahd_unpause(ahd);
  2894. }
  2895. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2896. tinfo, AHD_NEG_TO_GOAL);
  2897. if (update_needed && active)
  2898. ahd_update_pending_scbs(ahd);
  2899. }
  2900. /*
  2901. * Update the current state of tagged queuing for a given target.
  2902. */
  2903. void
  2904. ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2905. ahd_queue_alg alg)
  2906. {
  2907. ahd_platform_set_tags(ahd, devinfo, alg);
  2908. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2909. devinfo->lun, AC_TRANSFER_NEG, &alg);
  2910. }
  2911. static void
  2912. ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2913. struct ahd_transinfo *tinfo)
  2914. {
  2915. ahd_mode_state saved_modes;
  2916. u_int period;
  2917. u_int ppr_opts;
  2918. u_int con_opts;
  2919. u_int offset;
  2920. u_int saved_negoaddr;
  2921. uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
  2922. saved_modes = ahd_save_modes(ahd);
  2923. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2924. saved_negoaddr = ahd_inb(ahd, NEGOADDR);
  2925. ahd_outb(ahd, NEGOADDR, devinfo->target);
  2926. period = tinfo->period;
  2927. offset = tinfo->offset;
  2928. memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
  2929. ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
  2930. |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
  2931. con_opts = 0;
  2932. if (period == 0)
  2933. period = AHD_SYNCRATE_ASYNC;
  2934. if (period == AHD_SYNCRATE_160) {
  2935. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
  2936. /*
  2937. * When the SPI4 spec was finalized, PACE transfers
  2938. * was not made a configurable option in the PPR
  2939. * message. Instead it is assumed to be enabled for
  2940. * any syncrate faster than 80MHz. Nevertheless,
  2941. * Harpoon2A4 allows this to be configurable.
  2942. *
  2943. * Harpoon2A4 also assumes at most 2 data bytes per
  2944. * negotiated REQ/ACK offset. Paced transfers take
  2945. * 4, so we must adjust our offset.
  2946. */
  2947. ppr_opts |= PPROPT_PACE;
  2948. offset *= 2;
  2949. /*
  2950. * Harpoon2A assumed that there would be a
  2951. * fallback rate between 160MHz and 80Mhz,
  2952. * so 7 is used as the period factor rather
  2953. * than 8 for 160MHz.
  2954. */
  2955. period = AHD_SYNCRATE_REVA_160;
  2956. }
  2957. if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
  2958. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
  2959. ~AHD_PRECOMP_MASK;
  2960. } else {
  2961. /*
  2962. * Precomp should be disabled for non-paced transfers.
  2963. */
  2964. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
  2965. if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
  2966. && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0) {
  2967. /*
  2968. * Slow down our CRC interval to be
  2969. * compatible with devices that can't
  2970. * handle a CRC at full speed.
  2971. */
  2972. con_opts |= ENSLOWCRC;
  2973. }
  2974. }
  2975. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
  2976. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
  2977. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
  2978. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
  2979. ahd_outb(ahd, NEGPERIOD, period);
  2980. ahd_outb(ahd, NEGPPROPTS, ppr_opts);
  2981. ahd_outb(ahd, NEGOFFSET, offset);
  2982. if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
  2983. con_opts |= WIDEXFER;
  2984. /*
  2985. * During packetized transfers, the target will
  2986. * give us the oportunity to send command packets
  2987. * without us asserting attention.
  2988. */
  2989. if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  2990. con_opts |= ENAUTOATNO;
  2991. ahd_outb(ahd, NEGCONOPTS, con_opts);
  2992. ahd_outb(ahd, NEGOADDR, saved_negoaddr);
  2993. ahd_restore_modes(ahd, saved_modes);
  2994. }
  2995. /*
  2996. * When the transfer settings for a connection change, setup for
  2997. * negotiation in pending SCBs to effect the change as quickly as
  2998. * possible. We also cancel any negotiations that are scheduled
  2999. * for inflight SCBs that have not been started yet.
  3000. */
  3001. static void
  3002. ahd_update_pending_scbs(struct ahd_softc *ahd)
  3003. {
  3004. struct scb *pending_scb;
  3005. int pending_scb_count;
  3006. u_int scb_tag;
  3007. int paused;
  3008. u_int saved_scbptr;
  3009. ahd_mode_state saved_modes;
  3010. /*
  3011. * Traverse the pending SCB list and ensure that all of the
  3012. * SCBs there have the proper settings. We can only safely
  3013. * clear the negotiation required flag (setting requires the
  3014. * execution queue to be modified) and this is only possible
  3015. * if we are not already attempting to select out for this
  3016. * SCB. For this reason, all callers only call this routine
  3017. * if we are changing the negotiation settings for the currently
  3018. * active transaction on the bus.
  3019. */
  3020. pending_scb_count = 0;
  3021. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  3022. struct ahd_devinfo devinfo;
  3023. struct hardware_scb *pending_hscb;
  3024. struct ahd_initiator_tinfo *tinfo;
  3025. struct ahd_tmode_tstate *tstate;
  3026. ahd_scb_devinfo(ahd, &devinfo, pending_scb);
  3027. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  3028. devinfo.our_scsiid,
  3029. devinfo.target, &tstate);
  3030. pending_hscb = pending_scb->hscb;
  3031. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  3032. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  3033. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  3034. pending_hscb->control &= ~MK_MESSAGE;
  3035. }
  3036. ahd_sync_scb(ahd, pending_scb,
  3037. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  3038. pending_scb_count++;
  3039. }
  3040. if (pending_scb_count == 0)
  3041. return;
  3042. if (ahd_is_paused(ahd)) {
  3043. paused = 1;
  3044. } else {
  3045. paused = 0;
  3046. ahd_pause(ahd);
  3047. }
  3048. /*
  3049. * Force the sequencer to reinitialize the selection for
  3050. * the command at the head of the execution queue if it
  3051. * has already been setup. The negotiation changes may
  3052. * effect whether we select-out with ATN.
  3053. */
  3054. saved_modes = ahd_save_modes(ahd);
  3055. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3056. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  3057. saved_scbptr = ahd_get_scbptr(ahd);
  3058. /* Ensure that the hscbs down on the card match the new information */
  3059. for (scb_tag = 0; scb_tag < ahd->scb_data.maxhscbs; scb_tag++) {
  3060. struct hardware_scb *pending_hscb;
  3061. u_int control;
  3062. pending_scb = ahd_lookup_scb(ahd, scb_tag);
  3063. if (pending_scb == NULL)
  3064. continue;
  3065. ahd_set_scbptr(ahd, scb_tag);
  3066. pending_hscb = pending_scb->hscb;
  3067. control = ahd_inb_scbram(ahd, SCB_CONTROL);
  3068. control &= ~MK_MESSAGE;
  3069. control |= pending_hscb->control & MK_MESSAGE;
  3070. ahd_outb(ahd, SCB_CONTROL, control);
  3071. }
  3072. ahd_set_scbptr(ahd, saved_scbptr);
  3073. ahd_restore_modes(ahd, saved_modes);
  3074. if (paused == 0)
  3075. ahd_unpause(ahd);
  3076. }
  3077. /**************************** Pathing Information *****************************/
  3078. static void
  3079. ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3080. {
  3081. ahd_mode_state saved_modes;
  3082. u_int saved_scsiid;
  3083. role_t role;
  3084. int our_id;
  3085. saved_modes = ahd_save_modes(ahd);
  3086. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3087. if (ahd_inb(ahd, SSTAT0) & TARGET)
  3088. role = ROLE_TARGET;
  3089. else
  3090. role = ROLE_INITIATOR;
  3091. if (role == ROLE_TARGET
  3092. && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
  3093. /* We were selected, so pull our id from TARGIDIN */
  3094. our_id = ahd_inb(ahd, TARGIDIN) & OID;
  3095. } else if (role == ROLE_TARGET)
  3096. our_id = ahd_inb(ahd, TOWNID);
  3097. else
  3098. our_id = ahd_inb(ahd, IOWNID);
  3099. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  3100. ahd_compile_devinfo(devinfo,
  3101. our_id,
  3102. SCSIID_TARGET(ahd, saved_scsiid),
  3103. ahd_inb(ahd, SAVED_LUN),
  3104. SCSIID_CHANNEL(ahd, saved_scsiid),
  3105. role);
  3106. ahd_restore_modes(ahd, saved_modes);
  3107. }
  3108. void
  3109. ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3110. {
  3111. printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
  3112. devinfo->target, devinfo->lun);
  3113. }
  3114. struct ahd_phase_table_entry*
  3115. ahd_lookup_phase_entry(int phase)
  3116. {
  3117. struct ahd_phase_table_entry *entry;
  3118. struct ahd_phase_table_entry *last_entry;
  3119. /*
  3120. * num_phases doesn't include the default entry which
  3121. * will be returned if the phase doesn't match.
  3122. */
  3123. last_entry = &ahd_phase_table[num_phases];
  3124. for (entry = ahd_phase_table; entry < last_entry; entry++) {
  3125. if (phase == entry->phase)
  3126. break;
  3127. }
  3128. return (entry);
  3129. }
  3130. void
  3131. ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
  3132. u_int lun, char channel, role_t role)
  3133. {
  3134. devinfo->our_scsiid = our_id;
  3135. devinfo->target = target;
  3136. devinfo->lun = lun;
  3137. devinfo->target_offset = target;
  3138. devinfo->channel = channel;
  3139. devinfo->role = role;
  3140. if (channel == 'B')
  3141. devinfo->target_offset += 8;
  3142. devinfo->target_mask = (0x01 << devinfo->target_offset);
  3143. }
  3144. static void
  3145. ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3146. struct scb *scb)
  3147. {
  3148. role_t role;
  3149. int our_id;
  3150. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  3151. role = ROLE_INITIATOR;
  3152. if ((scb->hscb->control & TARGET_SCB) != 0)
  3153. role = ROLE_TARGET;
  3154. ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
  3155. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
  3156. }
  3157. /************************ Message Phase Processing ****************************/
  3158. /*
  3159. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  3160. * or enters the initial message out phase, we are interrupted. Fill our
  3161. * outgoing message buffer with the appropriate message and beging handing
  3162. * the message phase(s) manually.
  3163. */
  3164. static void
  3165. ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3166. struct scb *scb)
  3167. {
  3168. /*
  3169. * To facilitate adding multiple messages together,
  3170. * each routine should increment the index and len
  3171. * variables instead of setting them explicitly.
  3172. */
  3173. ahd->msgout_index = 0;
  3174. ahd->msgout_len = 0;
  3175. if (ahd_currently_packetized(ahd))
  3176. ahd->msg_flags |= MSG_FLAG_PACKETIZED;
  3177. if (ahd->send_msg_perror
  3178. && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
  3179. ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
  3180. ahd->msgout_len++;
  3181. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3182. #ifdef AHD_DEBUG
  3183. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3184. printf("Setting up for Parity Error delivery\n");
  3185. #endif
  3186. return;
  3187. } else if (scb == NULL) {
  3188. printf("%s: WARNING. No pending message for "
  3189. "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
  3190. ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
  3191. ahd->msgout_len++;
  3192. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3193. return;
  3194. }
  3195. if ((scb->flags & SCB_DEVICE_RESET) == 0
  3196. && (scb->flags & SCB_PACKETIZED) == 0
  3197. && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
  3198. u_int identify_msg;
  3199. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  3200. if ((scb->hscb->control & DISCENB) != 0)
  3201. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  3202. ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
  3203. ahd->msgout_len++;
  3204. if ((scb->hscb->control & TAG_ENB) != 0) {
  3205. ahd->msgout_buf[ahd->msgout_index++] =
  3206. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  3207. ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
  3208. ahd->msgout_len += 2;
  3209. }
  3210. }
  3211. if (scb->flags & SCB_DEVICE_RESET) {
  3212. ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
  3213. ahd->msgout_len++;
  3214. ahd_print_path(ahd, scb);
  3215. printf("Bus Device Reset Message Sent\n");
  3216. /*
  3217. * Clear our selection hardware in advance of
  3218. * the busfree. We may have an entry in the waiting
  3219. * Q for this target, and we don't want to go about
  3220. * selecting while we handle the busfree and blow it
  3221. * away.
  3222. */
  3223. ahd_outb(ahd, SCSISEQ0, 0);
  3224. } else if ((scb->flags & SCB_ABORT) != 0) {
  3225. if ((scb->hscb->control & TAG_ENB) != 0) {
  3226. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
  3227. } else {
  3228. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
  3229. }
  3230. ahd->msgout_len++;
  3231. ahd_print_path(ahd, scb);
  3232. printf("Abort%s Message Sent\n",
  3233. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  3234. /*
  3235. * Clear our selection hardware in advance of
  3236. * the busfree. We may have an entry in the waiting
  3237. * Q for this target, and we don't want to go about
  3238. * selecting while we handle the busfree and blow it
  3239. * away.
  3240. */
  3241. ahd_outb(ahd, SCSISEQ0, 0);
  3242. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  3243. ahd_build_transfer_msg(ahd, devinfo);
  3244. /*
  3245. * Clear our selection hardware in advance of potential
  3246. * PPR IU status change busfree. We may have an entry in
  3247. * the waiting Q for this target, and we don't want to go
  3248. * about selecting while we handle the busfree and blow
  3249. * it away.
  3250. */
  3251. ahd_outb(ahd, SCSISEQ0, 0);
  3252. } else {
  3253. printf("ahd_intr: AWAITING_MSG for an SCB that "
  3254. "does not have a waiting message\n");
  3255. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  3256. devinfo->target_mask);
  3257. panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
  3258. "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
  3259. ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
  3260. scb->flags);
  3261. }
  3262. /*
  3263. * Clear the MK_MESSAGE flag from the SCB so we aren't
  3264. * asked to send this message again.
  3265. */
  3266. ahd_outb(ahd, SCB_CONTROL,
  3267. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  3268. scb->hscb->control &= ~MK_MESSAGE;
  3269. ahd->msgout_index = 0;
  3270. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3271. }
  3272. /*
  3273. * Build an appropriate transfer negotiation message for the
  3274. * currently active target.
  3275. */
  3276. static void
  3277. ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3278. {
  3279. /*
  3280. * We need to initiate transfer negotiations.
  3281. * If our current and goal settings are identical,
  3282. * we want to renegotiate due to a check condition.
  3283. */
  3284. struct ahd_initiator_tinfo *tinfo;
  3285. struct ahd_tmode_tstate *tstate;
  3286. int dowide;
  3287. int dosync;
  3288. int doppr;
  3289. u_int period;
  3290. u_int ppr_options;
  3291. u_int offset;
  3292. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3293. devinfo->target, &tstate);
  3294. /*
  3295. * Filter our period based on the current connection.
  3296. * If we can't perform DT transfers on this segment (not in LVD
  3297. * mode for instance), then our decision to issue a PPR message
  3298. * may change.
  3299. */
  3300. period = tinfo->goal.period;
  3301. offset = tinfo->goal.offset;
  3302. ppr_options = tinfo->goal.ppr_options;
  3303. /* Target initiated PPR is not allowed in the SCSI spec */
  3304. if (devinfo->role == ROLE_TARGET)
  3305. ppr_options = 0;
  3306. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3307. &ppr_options, devinfo->role);
  3308. dowide = tinfo->curr.width != tinfo->goal.width;
  3309. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  3310. /*
  3311. * Only use PPR if we have options that need it, even if the device
  3312. * claims to support it. There might be an expander in the way
  3313. * that doesn't.
  3314. */
  3315. doppr = ppr_options != 0;
  3316. if (!dowide && !dosync && !doppr) {
  3317. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  3318. dosync = tinfo->goal.offset != 0;
  3319. }
  3320. if (!dowide && !dosync && !doppr) {
  3321. /*
  3322. * Force async with a WDTR message if we have a wide bus,
  3323. * or just issue an SDTR with a 0 offset.
  3324. */
  3325. if ((ahd->features & AHD_WIDE) != 0)
  3326. dowide = 1;
  3327. else
  3328. dosync = 1;
  3329. if (bootverbose) {
  3330. ahd_print_devinfo(ahd, devinfo);
  3331. printf("Ensuring async\n");
  3332. }
  3333. }
  3334. /* Target initiated PPR is not allowed in the SCSI spec */
  3335. if (devinfo->role == ROLE_TARGET)
  3336. doppr = 0;
  3337. /*
  3338. * Both the PPR message and SDTR message require the
  3339. * goal syncrate to be limited to what the target device
  3340. * is capable of handling (based on whether an LVD->SE
  3341. * expander is on the bus), so combine these two cases.
  3342. * Regardless, guarantee that if we are using WDTR and SDTR
  3343. * messages that WDTR comes first.
  3344. */
  3345. if (doppr || (dosync && !dowide)) {
  3346. offset = tinfo->goal.offset;
  3347. ahd_validate_offset(ahd, tinfo, period, &offset,
  3348. doppr ? tinfo->goal.width
  3349. : tinfo->curr.width,
  3350. devinfo->role);
  3351. if (doppr) {
  3352. ahd_construct_ppr(ahd, devinfo, period, offset,
  3353. tinfo->goal.width, ppr_options);
  3354. } else {
  3355. ahd_construct_sdtr(ahd, devinfo, period, offset);
  3356. }
  3357. } else {
  3358. ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
  3359. }
  3360. }
  3361. /*
  3362. * Build a synchronous negotiation message in our message
  3363. * buffer based on the input parameters.
  3364. */
  3365. static void
  3366. ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3367. u_int period, u_int offset)
  3368. {
  3369. if (offset == 0)
  3370. period = AHD_ASYNC_XFER_PERIOD;
  3371. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3372. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
  3373. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
  3374. ahd->msgout_buf[ahd->msgout_index++] = period;
  3375. ahd->msgout_buf[ahd->msgout_index++] = offset;
  3376. ahd->msgout_len += 5;
  3377. if (bootverbose) {
  3378. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  3379. ahd_name(ahd), devinfo->channel, devinfo->target,
  3380. devinfo->lun, period, offset);
  3381. }
  3382. }
  3383. /*
  3384. * Build a wide negotiateion message in our message
  3385. * buffer based on the input parameters.
  3386. */
  3387. static void
  3388. ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3389. u_int bus_width)
  3390. {
  3391. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3392. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
  3393. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
  3394. ahd->msgout_buf[ahd->msgout_index++] = bus_width;
  3395. ahd->msgout_len += 4;
  3396. if (bootverbose) {
  3397. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  3398. ahd_name(ahd), devinfo->channel, devinfo->target,
  3399. devinfo->lun, bus_width);
  3400. }
  3401. }
  3402. /*
  3403. * Build a parallel protocol request message in our message
  3404. * buffer based on the input parameters.
  3405. */
  3406. static void
  3407. ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3408. u_int period, u_int offset, u_int bus_width,
  3409. u_int ppr_options)
  3410. {
  3411. /*
  3412. * Always request precompensation from
  3413. * the other target if we are running
  3414. * at paced syncrates.
  3415. */
  3416. if (period <= AHD_SYNCRATE_PACED)
  3417. ppr_options |= MSG_EXT_PPR_PCOMP_EN;
  3418. if (offset == 0)
  3419. period = AHD_ASYNC_XFER_PERIOD;
  3420. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3421. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
  3422. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
  3423. ahd->msgout_buf[ahd->msgout_index++] = period;
  3424. ahd->msgout_buf[ahd->msgout_index++] = 0;
  3425. ahd->msgout_buf[ahd->msgout_index++] = offset;
  3426. ahd->msgout_buf[ahd->msgout_index++] = bus_width;
  3427. ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
  3428. ahd->msgout_len += 8;
  3429. if (bootverbose) {
  3430. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  3431. "offset %x, ppr_options %x\n", ahd_name(ahd),
  3432. devinfo->channel, devinfo->target, devinfo->lun,
  3433. bus_width, period, offset, ppr_options);
  3434. }
  3435. }
  3436. /*
  3437. * Clear any active message state.
  3438. */
  3439. static void
  3440. ahd_clear_msg_state(struct ahd_softc *ahd)
  3441. {
  3442. ahd_mode_state saved_modes;
  3443. saved_modes = ahd_save_modes(ahd);
  3444. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3445. ahd->send_msg_perror = 0;
  3446. ahd->msg_flags = MSG_FLAG_NONE;
  3447. ahd->msgout_len = 0;
  3448. ahd->msgin_index = 0;
  3449. ahd->msg_type = MSG_TYPE_NONE;
  3450. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  3451. /*
  3452. * The target didn't care to respond to our
  3453. * message request, so clear ATN.
  3454. */
  3455. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3456. }
  3457. ahd_outb(ahd, MSG_OUT, MSG_NOOP);
  3458. ahd_outb(ahd, SEQ_FLAGS2,
  3459. ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  3460. ahd_restore_modes(ahd, saved_modes);
  3461. }
  3462. /*
  3463. * Manual message loop handler.
  3464. */
  3465. static void
  3466. ahd_handle_message_phase(struct ahd_softc *ahd)
  3467. {
  3468. struct ahd_devinfo devinfo;
  3469. u_int bus_phase;
  3470. int end_session;
  3471. ahd_fetch_devinfo(ahd, &devinfo);
  3472. end_session = FALSE;
  3473. bus_phase = ahd_inb(ahd, LASTPHASE);
  3474. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
  3475. printf("LQIRETRY for LQIPHASE_OUTPKT\n");
  3476. ahd_outb(ahd, LQCTL2, LQIRETRY);
  3477. }
  3478. reswitch:
  3479. switch (ahd->msg_type) {
  3480. case MSG_TYPE_INITIATOR_MSGOUT:
  3481. {
  3482. int lastbyte;
  3483. int phasemis;
  3484. int msgdone;
  3485. if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
  3486. panic("HOST_MSG_LOOP interrupt with no active message");
  3487. #ifdef AHD_DEBUG
  3488. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3489. ahd_print_devinfo(ahd, &devinfo);
  3490. printf("INITIATOR_MSG_OUT");
  3491. }
  3492. #endif
  3493. phasemis = bus_phase != P_MESGOUT;
  3494. if (phasemis) {
  3495. #ifdef AHD_DEBUG
  3496. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3497. printf(" PHASEMIS %s\n",
  3498. ahd_lookup_phase_entry(bus_phase)
  3499. ->phasemsg);
  3500. }
  3501. #endif
  3502. if (bus_phase == P_MESGIN) {
  3503. /*
  3504. * Change gears and see if
  3505. * this messages is of interest to
  3506. * us or should be passed back to
  3507. * the sequencer.
  3508. */
  3509. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3510. ahd->send_msg_perror = 0;
  3511. ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  3512. ahd->msgin_index = 0;
  3513. goto reswitch;
  3514. }
  3515. end_session = TRUE;
  3516. break;
  3517. }
  3518. if (ahd->send_msg_perror) {
  3519. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3520. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3521. #ifdef AHD_DEBUG
  3522. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3523. printf(" byte 0x%x\n", ahd->send_msg_perror);
  3524. #endif
  3525. /*
  3526. * If we are notifying the target of a CRC error
  3527. * during packetized operations, the target is
  3528. * within its rights to acknowledge our message
  3529. * with a busfree.
  3530. */
  3531. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
  3532. && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
  3533. ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
  3534. ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
  3535. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3536. break;
  3537. }
  3538. msgdone = ahd->msgout_index == ahd->msgout_len;
  3539. if (msgdone) {
  3540. /*
  3541. * The target has requested a retry.
  3542. * Re-assert ATN, reset our message index to
  3543. * 0, and try again.
  3544. */
  3545. ahd->msgout_index = 0;
  3546. ahd_assert_atn(ahd);
  3547. }
  3548. lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
  3549. if (lastbyte) {
  3550. /* Last byte is signified by dropping ATN */
  3551. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3552. }
  3553. /*
  3554. * Clear our interrupt status and present
  3555. * the next byte on the bus.
  3556. */
  3557. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3558. #ifdef AHD_DEBUG
  3559. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3560. printf(" byte 0x%x\n",
  3561. ahd->msgout_buf[ahd->msgout_index]);
  3562. #endif
  3563. ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
  3564. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3565. break;
  3566. }
  3567. case MSG_TYPE_INITIATOR_MSGIN:
  3568. {
  3569. int phasemis;
  3570. int message_done;
  3571. #ifdef AHD_DEBUG
  3572. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3573. ahd_print_devinfo(ahd, &devinfo);
  3574. printf("INITIATOR_MSG_IN");
  3575. }
  3576. #endif
  3577. phasemis = bus_phase != P_MESGIN;
  3578. if (phasemis) {
  3579. #ifdef AHD_DEBUG
  3580. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3581. printf(" PHASEMIS %s\n",
  3582. ahd_lookup_phase_entry(bus_phase)
  3583. ->phasemsg);
  3584. }
  3585. #endif
  3586. ahd->msgin_index = 0;
  3587. if (bus_phase == P_MESGOUT
  3588. && (ahd->send_msg_perror != 0
  3589. || (ahd->msgout_len != 0
  3590. && ahd->msgout_index == 0))) {
  3591. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3592. goto reswitch;
  3593. }
  3594. end_session = TRUE;
  3595. break;
  3596. }
  3597. /* Pull the byte in without acking it */
  3598. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
  3599. #ifdef AHD_DEBUG
  3600. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3601. printf(" byte 0x%x\n",
  3602. ahd->msgin_buf[ahd->msgin_index]);
  3603. #endif
  3604. message_done = ahd_parse_msg(ahd, &devinfo);
  3605. if (message_done) {
  3606. /*
  3607. * Clear our incoming message buffer in case there
  3608. * is another message following this one.
  3609. */
  3610. ahd->msgin_index = 0;
  3611. /*
  3612. * If this message illicited a response,
  3613. * assert ATN so the target takes us to the
  3614. * message out phase.
  3615. */
  3616. if (ahd->msgout_len != 0) {
  3617. #ifdef AHD_DEBUG
  3618. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3619. ahd_print_devinfo(ahd, &devinfo);
  3620. printf("Asserting ATN for response\n");
  3621. }
  3622. #endif
  3623. ahd_assert_atn(ahd);
  3624. }
  3625. } else
  3626. ahd->msgin_index++;
  3627. if (message_done == MSGLOOP_TERMINATED) {
  3628. end_session = TRUE;
  3629. } else {
  3630. /* Ack the byte */
  3631. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3632. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
  3633. }
  3634. break;
  3635. }
  3636. case MSG_TYPE_TARGET_MSGIN:
  3637. {
  3638. int msgdone;
  3639. int msgout_request;
  3640. /*
  3641. * By default, the message loop will continue.
  3642. */
  3643. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3644. if (ahd->msgout_len == 0)
  3645. panic("Target MSGIN with no active message");
  3646. /*
  3647. * If we interrupted a mesgout session, the initiator
  3648. * will not know this until our first REQ. So, we
  3649. * only honor mesgout requests after we've sent our
  3650. * first byte.
  3651. */
  3652. if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
  3653. && ahd->msgout_index > 0)
  3654. msgout_request = TRUE;
  3655. else
  3656. msgout_request = FALSE;
  3657. if (msgout_request) {
  3658. /*
  3659. * Change gears and see if
  3660. * this messages is of interest to
  3661. * us or should be passed back to
  3662. * the sequencer.
  3663. */
  3664. ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
  3665. ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
  3666. ahd->msgin_index = 0;
  3667. /* Dummy read to REQ for first byte */
  3668. ahd_inb(ahd, SCSIDAT);
  3669. ahd_outb(ahd, SXFRCTL0,
  3670. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3671. break;
  3672. }
  3673. msgdone = ahd->msgout_index == ahd->msgout_len;
  3674. if (msgdone) {
  3675. ahd_outb(ahd, SXFRCTL0,
  3676. ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3677. end_session = TRUE;
  3678. break;
  3679. }
  3680. /*
  3681. * Present the next byte on the bus.
  3682. */
  3683. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3684. ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
  3685. break;
  3686. }
  3687. case MSG_TYPE_TARGET_MSGOUT:
  3688. {
  3689. int lastbyte;
  3690. int msgdone;
  3691. /*
  3692. * By default, the message loop will continue.
  3693. */
  3694. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3695. /*
  3696. * The initiator signals that this is
  3697. * the last byte by dropping ATN.
  3698. */
  3699. lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
  3700. /*
  3701. * Read the latched byte, but turn off SPIOEN first
  3702. * so that we don't inadvertently cause a REQ for the
  3703. * next byte.
  3704. */
  3705. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3706. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
  3707. msgdone = ahd_parse_msg(ahd, &devinfo);
  3708. if (msgdone == MSGLOOP_TERMINATED) {
  3709. /*
  3710. * The message is *really* done in that it caused
  3711. * us to go to bus free. The sequencer has already
  3712. * been reset at this point, so pull the ejection
  3713. * handle.
  3714. */
  3715. return;
  3716. }
  3717. ahd->msgin_index++;
  3718. /*
  3719. * XXX Read spec about initiator dropping ATN too soon
  3720. * and use msgdone to detect it.
  3721. */
  3722. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  3723. ahd->msgin_index = 0;
  3724. /*
  3725. * If this message illicited a response, transition
  3726. * to the Message in phase and send it.
  3727. */
  3728. if (ahd->msgout_len != 0) {
  3729. ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
  3730. ahd_outb(ahd, SXFRCTL0,
  3731. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3732. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  3733. ahd->msgin_index = 0;
  3734. break;
  3735. }
  3736. }
  3737. if (lastbyte)
  3738. end_session = TRUE;
  3739. else {
  3740. /* Ask for the next byte. */
  3741. ahd_outb(ahd, SXFRCTL0,
  3742. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3743. }
  3744. break;
  3745. }
  3746. default:
  3747. panic("Unknown REQINIT message type");
  3748. }
  3749. if (end_session) {
  3750. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
  3751. printf("%s: Returning to Idle Loop\n",
  3752. ahd_name(ahd));
  3753. ahd_clear_msg_state(ahd);
  3754. /*
  3755. * Perform the equivalent of a clear_target_state.
  3756. */
  3757. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  3758. ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT);
  3759. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  3760. } else {
  3761. ahd_clear_msg_state(ahd);
  3762. ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
  3763. }
  3764. }
  3765. }
  3766. /*
  3767. * See if we sent a particular extended message to the target.
  3768. * If "full" is true, return true only if the target saw the full
  3769. * message. If "full" is false, return true if the target saw at
  3770. * least the first byte of the message.
  3771. */
  3772. static int
  3773. ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
  3774. {
  3775. int found;
  3776. u_int index;
  3777. found = FALSE;
  3778. index = 0;
  3779. while (index < ahd->msgout_len) {
  3780. if (ahd->msgout_buf[index] == MSG_EXTENDED) {
  3781. u_int end_index;
  3782. end_index = index + 1 + ahd->msgout_buf[index + 1];
  3783. if (ahd->msgout_buf[index+2] == msgval
  3784. && type == AHDMSG_EXT) {
  3785. if (full) {
  3786. if (ahd->msgout_index > end_index)
  3787. found = TRUE;
  3788. } else if (ahd->msgout_index > index)
  3789. found = TRUE;
  3790. }
  3791. index = end_index;
  3792. } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
  3793. && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  3794. /* Skip tag type and tag id or residue param*/
  3795. index += 2;
  3796. } else {
  3797. /* Single byte message */
  3798. if (type == AHDMSG_1B
  3799. && ahd->msgout_index > index
  3800. && (ahd->msgout_buf[index] == msgval
  3801. || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
  3802. && msgval == MSG_IDENTIFYFLAG)))
  3803. found = TRUE;
  3804. index++;
  3805. }
  3806. if (found)
  3807. break;
  3808. }
  3809. return (found);
  3810. }
  3811. /*
  3812. * Wait for a complete incoming message, parse it, and respond accordingly.
  3813. */
  3814. static int
  3815. ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3816. {
  3817. struct ahd_initiator_tinfo *tinfo;
  3818. struct ahd_tmode_tstate *tstate;
  3819. int reject;
  3820. int done;
  3821. int response;
  3822. done = MSGLOOP_IN_PROG;
  3823. response = FALSE;
  3824. reject = FALSE;
  3825. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3826. devinfo->target, &tstate);
  3827. /*
  3828. * Parse as much of the message as is available,
  3829. * rejecting it if we don't support it. When
  3830. * the entire message is available and has been
  3831. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  3832. * that we have parsed an entire message.
  3833. *
  3834. * In the case of extended messages, we accept the length
  3835. * byte outright and perform more checking once we know the
  3836. * extended message type.
  3837. */
  3838. switch (ahd->msgin_buf[0]) {
  3839. case MSG_DISCONNECT:
  3840. case MSG_SAVEDATAPOINTER:
  3841. case MSG_CMDCOMPLETE:
  3842. case MSG_RESTOREPOINTERS:
  3843. case MSG_IGN_WIDE_RESIDUE:
  3844. /*
  3845. * End our message loop as these are messages
  3846. * the sequencer handles on its own.
  3847. */
  3848. done = MSGLOOP_TERMINATED;
  3849. break;
  3850. case MSG_MESSAGE_REJECT:
  3851. response = ahd_handle_msg_reject(ahd, devinfo);
  3852. /* FALLTHROUGH */
  3853. case MSG_NOOP:
  3854. done = MSGLOOP_MSGCOMPLETE;
  3855. break;
  3856. case MSG_EXTENDED:
  3857. {
  3858. /* Wait for enough of the message to begin validation */
  3859. if (ahd->msgin_index < 2)
  3860. break;
  3861. switch (ahd->msgin_buf[2]) {
  3862. case MSG_EXT_SDTR:
  3863. {
  3864. u_int period;
  3865. u_int ppr_options;
  3866. u_int offset;
  3867. u_int saved_offset;
  3868. if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  3869. reject = TRUE;
  3870. break;
  3871. }
  3872. /*
  3873. * Wait until we have both args before validating
  3874. * and acting on this message.
  3875. *
  3876. * Add one to MSG_EXT_SDTR_LEN to account for
  3877. * the extended message preamble.
  3878. */
  3879. if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  3880. break;
  3881. period = ahd->msgin_buf[3];
  3882. ppr_options = 0;
  3883. saved_offset = offset = ahd->msgin_buf[4];
  3884. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3885. &ppr_options, devinfo->role);
  3886. ahd_validate_offset(ahd, tinfo, period, &offset,
  3887. tinfo->curr.width, devinfo->role);
  3888. if (bootverbose) {
  3889. printf("(%s:%c:%d:%d): Received "
  3890. "SDTR period %x, offset %x\n\t"
  3891. "Filtered to period %x, offset %x\n",
  3892. ahd_name(ahd), devinfo->channel,
  3893. devinfo->target, devinfo->lun,
  3894. ahd->msgin_buf[3], saved_offset,
  3895. period, offset);
  3896. }
  3897. ahd_set_syncrate(ahd, devinfo, period,
  3898. offset, ppr_options,
  3899. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  3900. /*paused*/TRUE);
  3901. /*
  3902. * See if we initiated Sync Negotiation
  3903. * and didn't have to fall down to async
  3904. * transfers.
  3905. */
  3906. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  3907. /* We started it */
  3908. if (saved_offset != offset) {
  3909. /* Went too low - force async */
  3910. reject = TRUE;
  3911. }
  3912. } else {
  3913. /*
  3914. * Send our own SDTR in reply
  3915. */
  3916. if (bootverbose
  3917. && devinfo->role == ROLE_INITIATOR) {
  3918. printf("(%s:%c:%d:%d): Target "
  3919. "Initiated SDTR\n",
  3920. ahd_name(ahd), devinfo->channel,
  3921. devinfo->target, devinfo->lun);
  3922. }
  3923. ahd->msgout_index = 0;
  3924. ahd->msgout_len = 0;
  3925. ahd_construct_sdtr(ahd, devinfo,
  3926. period, offset);
  3927. ahd->msgout_index = 0;
  3928. response = TRUE;
  3929. }
  3930. done = MSGLOOP_MSGCOMPLETE;
  3931. break;
  3932. }
  3933. case MSG_EXT_WDTR:
  3934. {
  3935. u_int bus_width;
  3936. u_int saved_width;
  3937. u_int sending_reply;
  3938. sending_reply = FALSE;
  3939. if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  3940. reject = TRUE;
  3941. break;
  3942. }
  3943. /*
  3944. * Wait until we have our arg before validating
  3945. * and acting on this message.
  3946. *
  3947. * Add one to MSG_EXT_WDTR_LEN to account for
  3948. * the extended message preamble.
  3949. */
  3950. if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  3951. break;
  3952. bus_width = ahd->msgin_buf[3];
  3953. saved_width = bus_width;
  3954. ahd_validate_width(ahd, tinfo, &bus_width,
  3955. devinfo->role);
  3956. if (bootverbose) {
  3957. printf("(%s:%c:%d:%d): Received WDTR "
  3958. "%x filtered to %x\n",
  3959. ahd_name(ahd), devinfo->channel,
  3960. devinfo->target, devinfo->lun,
  3961. saved_width, bus_width);
  3962. }
  3963. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  3964. /*
  3965. * Don't send a WDTR back to the
  3966. * target, since we asked first.
  3967. * If the width went higher than our
  3968. * request, reject it.
  3969. */
  3970. if (saved_width > bus_width) {
  3971. reject = TRUE;
  3972. printf("(%s:%c:%d:%d): requested %dBit "
  3973. "transfers. Rejecting...\n",
  3974. ahd_name(ahd), devinfo->channel,
  3975. devinfo->target, devinfo->lun,
  3976. 8 * (0x01 << bus_width));
  3977. bus_width = 0;
  3978. }
  3979. } else {
  3980. /*
  3981. * Send our own WDTR in reply
  3982. */
  3983. if (bootverbose
  3984. && devinfo->role == ROLE_INITIATOR) {
  3985. printf("(%s:%c:%d:%d): Target "
  3986. "Initiated WDTR\n",
  3987. ahd_name(ahd), devinfo->channel,
  3988. devinfo->target, devinfo->lun);
  3989. }
  3990. ahd->msgout_index = 0;
  3991. ahd->msgout_len = 0;
  3992. ahd_construct_wdtr(ahd, devinfo, bus_width);
  3993. ahd->msgout_index = 0;
  3994. response = TRUE;
  3995. sending_reply = TRUE;
  3996. }
  3997. /*
  3998. * After a wide message, we are async, but
  3999. * some devices don't seem to honor this portion
  4000. * of the spec. Force a renegotiation of the
  4001. * sync component of our transfer agreement even
  4002. * if our goal is async. By updating our width
  4003. * after forcing the negotiation, we avoid
  4004. * renegotiating for width.
  4005. */
  4006. ahd_update_neg_request(ahd, devinfo, tstate,
  4007. tinfo, AHD_NEG_ALWAYS);
  4008. ahd_set_width(ahd, devinfo, bus_width,
  4009. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4010. /*paused*/TRUE);
  4011. if (sending_reply == FALSE && reject == FALSE) {
  4012. /*
  4013. * We will always have an SDTR to send.
  4014. */
  4015. ahd->msgout_index = 0;
  4016. ahd->msgout_len = 0;
  4017. ahd_build_transfer_msg(ahd, devinfo);
  4018. ahd->msgout_index = 0;
  4019. response = TRUE;
  4020. }
  4021. done = MSGLOOP_MSGCOMPLETE;
  4022. break;
  4023. }
  4024. case MSG_EXT_PPR:
  4025. {
  4026. u_int period;
  4027. u_int offset;
  4028. u_int bus_width;
  4029. u_int ppr_options;
  4030. u_int saved_width;
  4031. u_int saved_offset;
  4032. u_int saved_ppr_options;
  4033. if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  4034. reject = TRUE;
  4035. break;
  4036. }
  4037. /*
  4038. * Wait until we have all args before validating
  4039. * and acting on this message.
  4040. *
  4041. * Add one to MSG_EXT_PPR_LEN to account for
  4042. * the extended message preamble.
  4043. */
  4044. if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
  4045. break;
  4046. period = ahd->msgin_buf[3];
  4047. offset = ahd->msgin_buf[5];
  4048. bus_width = ahd->msgin_buf[6];
  4049. saved_width = bus_width;
  4050. ppr_options = ahd->msgin_buf[7];
  4051. /*
  4052. * According to the spec, a DT only
  4053. * period factor with no DT option
  4054. * set implies async.
  4055. */
  4056. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  4057. && period <= 9)
  4058. offset = 0;
  4059. saved_ppr_options = ppr_options;
  4060. saved_offset = offset;
  4061. /*
  4062. * Transfer options are only available if we
  4063. * are negotiating wide.
  4064. */
  4065. if (bus_width == 0)
  4066. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  4067. ahd_validate_width(ahd, tinfo, &bus_width,
  4068. devinfo->role);
  4069. ahd_devlimited_syncrate(ahd, tinfo, &period,
  4070. &ppr_options, devinfo->role);
  4071. ahd_validate_offset(ahd, tinfo, period, &offset,
  4072. bus_width, devinfo->role);
  4073. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
  4074. /*
  4075. * If we are unable to do any of the
  4076. * requested options (we went too low),
  4077. * then we'll have to reject the message.
  4078. */
  4079. if (saved_width > bus_width
  4080. || saved_offset != offset
  4081. || saved_ppr_options != ppr_options) {
  4082. reject = TRUE;
  4083. period = 0;
  4084. offset = 0;
  4085. bus_width = 0;
  4086. ppr_options = 0;
  4087. }
  4088. } else {
  4089. if (devinfo->role != ROLE_TARGET)
  4090. printf("(%s:%c:%d:%d): Target "
  4091. "Initiated PPR\n",
  4092. ahd_name(ahd), devinfo->channel,
  4093. devinfo->target, devinfo->lun);
  4094. else
  4095. printf("(%s:%c:%d:%d): Initiator "
  4096. "Initiated PPR\n",
  4097. ahd_name(ahd), devinfo->channel,
  4098. devinfo->target, devinfo->lun);
  4099. ahd->msgout_index = 0;
  4100. ahd->msgout_len = 0;
  4101. ahd_construct_ppr(ahd, devinfo, period, offset,
  4102. bus_width, ppr_options);
  4103. ahd->msgout_index = 0;
  4104. response = TRUE;
  4105. }
  4106. if (bootverbose) {
  4107. printf("(%s:%c:%d:%d): Received PPR width %x, "
  4108. "period %x, offset %x,options %x\n"
  4109. "\tFiltered to width %x, period %x, "
  4110. "offset %x, options %x\n",
  4111. ahd_name(ahd), devinfo->channel,
  4112. devinfo->target, devinfo->lun,
  4113. saved_width, ahd->msgin_buf[3],
  4114. saved_offset, saved_ppr_options,
  4115. bus_width, period, offset, ppr_options);
  4116. }
  4117. ahd_set_width(ahd, devinfo, bus_width,
  4118. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4119. /*paused*/TRUE);
  4120. ahd_set_syncrate(ahd, devinfo, period,
  4121. offset, ppr_options,
  4122. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4123. /*paused*/TRUE);
  4124. done = MSGLOOP_MSGCOMPLETE;
  4125. break;
  4126. }
  4127. default:
  4128. /* Unknown extended message. Reject it. */
  4129. reject = TRUE;
  4130. break;
  4131. }
  4132. break;
  4133. }
  4134. #ifdef AHD_TARGET_MODE
  4135. case MSG_BUS_DEV_RESET:
  4136. ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
  4137. CAM_BDR_SENT,
  4138. "Bus Device Reset Received",
  4139. /*verbose_level*/0);
  4140. ahd_restart(ahd);
  4141. done = MSGLOOP_TERMINATED;
  4142. break;
  4143. case MSG_ABORT_TAG:
  4144. case MSG_ABORT:
  4145. case MSG_CLEAR_QUEUE:
  4146. {
  4147. int tag;
  4148. /* Target mode messages */
  4149. if (devinfo->role != ROLE_TARGET) {
  4150. reject = TRUE;
  4151. break;
  4152. }
  4153. tag = SCB_LIST_NULL;
  4154. if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
  4155. tag = ahd_inb(ahd, INITIATOR_TAG);
  4156. ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4157. devinfo->lun, tag, ROLE_TARGET,
  4158. CAM_REQ_ABORTED);
  4159. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4160. if (tstate != NULL) {
  4161. struct ahd_tmode_lstate* lstate;
  4162. lstate = tstate->enabled_luns[devinfo->lun];
  4163. if (lstate != NULL) {
  4164. ahd_queue_lstate_event(ahd, lstate,
  4165. devinfo->our_scsiid,
  4166. ahd->msgin_buf[0],
  4167. /*arg*/tag);
  4168. ahd_send_lstate_events(ahd, lstate);
  4169. }
  4170. }
  4171. ahd_restart(ahd);
  4172. done = MSGLOOP_TERMINATED;
  4173. break;
  4174. }
  4175. #endif
  4176. case MSG_QAS_REQUEST:
  4177. #ifdef AHD_DEBUG
  4178. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  4179. printf("%s: QAS request. SCSISIGI == 0x%x\n",
  4180. ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
  4181. #endif
  4182. ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
  4183. /* FALLTHROUGH */
  4184. case MSG_TERM_IO_PROC:
  4185. default:
  4186. reject = TRUE;
  4187. break;
  4188. }
  4189. if (reject) {
  4190. /*
  4191. * Setup to reject the message.
  4192. */
  4193. ahd->msgout_index = 0;
  4194. ahd->msgout_len = 1;
  4195. ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
  4196. done = MSGLOOP_MSGCOMPLETE;
  4197. response = TRUE;
  4198. }
  4199. if (done != MSGLOOP_IN_PROG && !response)
  4200. /* Clear the outgoing message buffer */
  4201. ahd->msgout_len = 0;
  4202. return (done);
  4203. }
  4204. /*
  4205. * Process a message reject message.
  4206. */
  4207. static int
  4208. ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4209. {
  4210. /*
  4211. * What we care about here is if we had an
  4212. * outstanding SDTR or WDTR message for this
  4213. * target. If we did, this is a signal that
  4214. * the target is refusing negotiation.
  4215. */
  4216. struct scb *scb;
  4217. struct ahd_initiator_tinfo *tinfo;
  4218. struct ahd_tmode_tstate *tstate;
  4219. u_int scb_index;
  4220. u_int last_msg;
  4221. int response = 0;
  4222. scb_index = ahd_get_scbptr(ahd);
  4223. scb = ahd_lookup_scb(ahd, scb_index);
  4224. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
  4225. devinfo->our_scsiid,
  4226. devinfo->target, &tstate);
  4227. /* Might be necessary */
  4228. last_msg = ahd_inb(ahd, LAST_MSG);
  4229. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  4230. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
  4231. && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
  4232. /*
  4233. * Target may not like our SPI-4 PPR Options.
  4234. * Attempt to negotiate 80MHz which will turn
  4235. * off these options.
  4236. */
  4237. if (bootverbose) {
  4238. printf("(%s:%c:%d:%d): PPR Rejected. "
  4239. "Trying simple U160 PPR\n",
  4240. ahd_name(ahd), devinfo->channel,
  4241. devinfo->target, devinfo->lun);
  4242. }
  4243. tinfo->goal.period = AHD_SYNCRATE_DT;
  4244. tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
  4245. | MSG_EXT_PPR_QAS_REQ
  4246. | MSG_EXT_PPR_DT_REQ;
  4247. } else {
  4248. /*
  4249. * Target does not support the PPR message.
  4250. * Attempt to negotiate SPI-2 style.
  4251. */
  4252. if (bootverbose) {
  4253. printf("(%s:%c:%d:%d): PPR Rejected. "
  4254. "Trying WDTR/SDTR\n",
  4255. ahd_name(ahd), devinfo->channel,
  4256. devinfo->target, devinfo->lun);
  4257. }
  4258. tinfo->goal.ppr_options = 0;
  4259. tinfo->curr.transport_version = 2;
  4260. tinfo->goal.transport_version = 2;
  4261. }
  4262. ahd->msgout_index = 0;
  4263. ahd->msgout_len = 0;
  4264. ahd_build_transfer_msg(ahd, devinfo);
  4265. ahd->msgout_index = 0;
  4266. response = 1;
  4267. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  4268. /* note 8bit xfers */
  4269. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  4270. "8bit transfers\n", ahd_name(ahd),
  4271. devinfo->channel, devinfo->target, devinfo->lun);
  4272. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4273. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4274. /*paused*/TRUE);
  4275. /*
  4276. * No need to clear the sync rate. If the target
  4277. * did not accept the command, our syncrate is
  4278. * unaffected. If the target started the negotiation,
  4279. * but rejected our response, we already cleared the
  4280. * sync rate before sending our WDTR.
  4281. */
  4282. if (tinfo->goal.offset != tinfo->curr.offset) {
  4283. /* Start the sync negotiation */
  4284. ahd->msgout_index = 0;
  4285. ahd->msgout_len = 0;
  4286. ahd_build_transfer_msg(ahd, devinfo);
  4287. ahd->msgout_index = 0;
  4288. response = 1;
  4289. }
  4290. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  4291. /* note asynch xfers and clear flag */
  4292. ahd_set_syncrate(ahd, devinfo, /*period*/0,
  4293. /*offset*/0, /*ppr_options*/0,
  4294. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4295. /*paused*/TRUE);
  4296. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  4297. "Using asynchronous transfers\n",
  4298. ahd_name(ahd), devinfo->channel,
  4299. devinfo->target, devinfo->lun);
  4300. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  4301. int tag_type;
  4302. int mask;
  4303. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  4304. if (tag_type == MSG_SIMPLE_TASK) {
  4305. printf("(%s:%c:%d:%d): refuses tagged commands. "
  4306. "Performing non-tagged I/O\n", ahd_name(ahd),
  4307. devinfo->channel, devinfo->target, devinfo->lun);
  4308. ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE);
  4309. mask = ~0x23;
  4310. } else {
  4311. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  4312. "Performing simple queue tagged I/O only\n",
  4313. ahd_name(ahd), devinfo->channel, devinfo->target,
  4314. devinfo->lun, tag_type == MSG_ORDERED_TASK
  4315. ? "ordered" : "head of queue");
  4316. ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC);
  4317. mask = ~0x03;
  4318. }
  4319. /*
  4320. * Resend the identify for this CCB as the target
  4321. * may believe that the selection is invalid otherwise.
  4322. */
  4323. ahd_outb(ahd, SCB_CONTROL,
  4324. ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
  4325. scb->hscb->control &= mask;
  4326. ahd_set_transaction_tag(scb, /*enabled*/FALSE,
  4327. /*type*/MSG_SIMPLE_TASK);
  4328. ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
  4329. ahd_assert_atn(ahd);
  4330. ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  4331. SCB_GET_TAG(scb));
  4332. /*
  4333. * Requeue all tagged commands for this target
  4334. * currently in our posession so they can be
  4335. * converted to untagged commands.
  4336. */
  4337. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  4338. SCB_GET_CHANNEL(ahd, scb),
  4339. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  4340. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  4341. SEARCH_COMPLETE);
  4342. } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
  4343. /*
  4344. * Most likely the device believes that we had
  4345. * previously negotiated packetized.
  4346. */
  4347. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  4348. | MSG_FLAG_IU_REQ_CHANGED;
  4349. ahd_force_renegotiation(ahd, devinfo);
  4350. ahd->msgout_index = 0;
  4351. ahd->msgout_len = 0;
  4352. ahd_build_transfer_msg(ahd, devinfo);
  4353. ahd->msgout_index = 0;
  4354. response = 1;
  4355. } else {
  4356. /*
  4357. * Otherwise, we ignore it.
  4358. */
  4359. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  4360. ahd_name(ahd), devinfo->channel, devinfo->target,
  4361. last_msg);
  4362. }
  4363. return (response);
  4364. }
  4365. /*
  4366. * Process an ingnore wide residue message.
  4367. */
  4368. static void
  4369. ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4370. {
  4371. u_int scb_index;
  4372. struct scb *scb;
  4373. scb_index = ahd_get_scbptr(ahd);
  4374. scb = ahd_lookup_scb(ahd, scb_index);
  4375. /*
  4376. * XXX Actually check data direction in the sequencer?
  4377. * Perhaps add datadir to some spare bits in the hscb?
  4378. */
  4379. if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
  4380. || ahd_get_transfer_dir(scb) != CAM_DIR_IN) {
  4381. /*
  4382. * Ignore the message if we haven't
  4383. * seen an appropriate data phase yet.
  4384. */
  4385. } else {
  4386. /*
  4387. * If the residual occurred on the last
  4388. * transfer and the transfer request was
  4389. * expected to end on an odd count, do
  4390. * nothing. Otherwise, subtract a byte
  4391. * and update the residual count accordingly.
  4392. */
  4393. uint32_t sgptr;
  4394. sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4395. if ((sgptr & SG_LIST_NULL) != 0
  4396. && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4397. & SCB_XFERLEN_ODD) != 0) {
  4398. /*
  4399. * If the residual occurred on the last
  4400. * transfer and the transfer request was
  4401. * expected to end on an odd count, do
  4402. * nothing.
  4403. */
  4404. } else {
  4405. uint32_t data_cnt;
  4406. uint64_t data_addr;
  4407. uint32_t sglen;
  4408. /* Pull in the rest of the sgptr */
  4409. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4410. data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4411. if ((sgptr & SG_LIST_NULL) != 0) {
  4412. /*
  4413. * The residual data count is not updated
  4414. * for the command run to completion case.
  4415. * Explicitly zero the count.
  4416. */
  4417. data_cnt &= ~AHD_SG_LEN_MASK;
  4418. }
  4419. data_addr = ahd_inq(ahd, SHADDR);
  4420. data_cnt += 1;
  4421. data_addr -= 1;
  4422. sgptr &= SG_PTR_MASK;
  4423. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4424. struct ahd_dma64_seg *sg;
  4425. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4426. /*
  4427. * The residual sg ptr points to the next S/G
  4428. * to load so we must go back one.
  4429. */
  4430. sg--;
  4431. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4432. if (sg != scb->sg_list
  4433. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4434. sg--;
  4435. sglen = ahd_le32toh(sg->len);
  4436. /*
  4437. * Preserve High Address and SG_LIST
  4438. * bits while setting the count to 1.
  4439. */
  4440. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4441. data_addr = ahd_le64toh(sg->addr)
  4442. + (sglen & AHD_SG_LEN_MASK)
  4443. - 1;
  4444. /*
  4445. * Increment sg so it points to the
  4446. * "next" sg.
  4447. */
  4448. sg++;
  4449. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4450. sg);
  4451. }
  4452. } else {
  4453. struct ahd_dma_seg *sg;
  4454. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4455. /*
  4456. * The residual sg ptr points to the next S/G
  4457. * to load so we must go back one.
  4458. */
  4459. sg--;
  4460. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4461. if (sg != scb->sg_list
  4462. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4463. sg--;
  4464. sglen = ahd_le32toh(sg->len);
  4465. /*
  4466. * Preserve High Address and SG_LIST
  4467. * bits while setting the count to 1.
  4468. */
  4469. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4470. data_addr = ahd_le32toh(sg->addr)
  4471. + (sglen & AHD_SG_LEN_MASK)
  4472. - 1;
  4473. /*
  4474. * Increment sg so it points to the
  4475. * "next" sg.
  4476. */
  4477. sg++;
  4478. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4479. sg);
  4480. }
  4481. }
  4482. /*
  4483. * Toggle the "oddness" of the transfer length
  4484. * to handle this mid-transfer ignore wide
  4485. * residue. This ensures that the oddness is
  4486. * correct for subsequent data transfers.
  4487. */
  4488. ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
  4489. ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4490. ^ SCB_XFERLEN_ODD);
  4491. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  4492. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
  4493. /*
  4494. * The FIFO's pointers will be updated if/when the
  4495. * sequencer re-enters a data phase.
  4496. */
  4497. }
  4498. }
  4499. }
  4500. /*
  4501. * Reinitialize the data pointers for the active transfer
  4502. * based on its current residual.
  4503. */
  4504. static void
  4505. ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
  4506. {
  4507. struct scb *scb;
  4508. ahd_mode_state saved_modes;
  4509. u_int scb_index;
  4510. u_int wait;
  4511. uint32_t sgptr;
  4512. uint32_t resid;
  4513. uint64_t dataptr;
  4514. AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
  4515. AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
  4516. scb_index = ahd_get_scbptr(ahd);
  4517. scb = ahd_lookup_scb(ahd, scb_index);
  4518. /*
  4519. * Release and reacquire the FIFO so we
  4520. * have a clean slate.
  4521. */
  4522. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  4523. wait = 1000;
  4524. while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE))
  4525. ahd_delay(100);
  4526. if (wait == 0) {
  4527. ahd_print_path(ahd, scb);
  4528. printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
  4529. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  4530. }
  4531. saved_modes = ahd_save_modes(ahd);
  4532. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4533. ahd_outb(ahd, DFFSTAT,
  4534. ahd_inb(ahd, DFFSTAT)
  4535. | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
  4536. /*
  4537. * Determine initial values for data_addr and data_cnt
  4538. * for resuming the data phase.
  4539. */
  4540. sgptr = (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 3) << 24)
  4541. | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 2) << 16)
  4542. | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 1) << 8)
  4543. | ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4544. sgptr &= SG_PTR_MASK;
  4545. resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
  4546. | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
  4547. | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4548. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4549. struct ahd_dma64_seg *sg;
  4550. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4551. /* The residual sg_ptr always points to the next sg */
  4552. sg--;
  4553. dataptr = ahd_le64toh(sg->addr)
  4554. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4555. - resid;
  4556. ahd_outb(ahd, HADDR + 7, dataptr >> 56);
  4557. ahd_outb(ahd, HADDR + 6, dataptr >> 48);
  4558. ahd_outb(ahd, HADDR + 5, dataptr >> 40);
  4559. ahd_outb(ahd, HADDR + 4, dataptr >> 32);
  4560. } else {
  4561. struct ahd_dma_seg *sg;
  4562. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4563. /* The residual sg_ptr always points to the next sg */
  4564. sg--;
  4565. dataptr = ahd_le32toh(sg->addr)
  4566. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4567. - resid;
  4568. ahd_outb(ahd, HADDR + 4,
  4569. (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
  4570. }
  4571. ahd_outb(ahd, HADDR + 3, dataptr >> 24);
  4572. ahd_outb(ahd, HADDR + 2, dataptr >> 16);
  4573. ahd_outb(ahd, HADDR + 1, dataptr >> 8);
  4574. ahd_outb(ahd, HADDR, dataptr);
  4575. ahd_outb(ahd, HCNT + 2, resid >> 16);
  4576. ahd_outb(ahd, HCNT + 1, resid >> 8);
  4577. ahd_outb(ahd, HCNT, resid);
  4578. }
  4579. /*
  4580. * Handle the effects of issuing a bus device reset message.
  4581. */
  4582. static void
  4583. ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4584. u_int lun, cam_status status, char *message,
  4585. int verbose_level)
  4586. {
  4587. #ifdef AHD_TARGET_MODE
  4588. struct ahd_tmode_tstate* tstate;
  4589. #endif
  4590. int found;
  4591. found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4592. lun, SCB_LIST_NULL, devinfo->role,
  4593. status);
  4594. #ifdef AHD_TARGET_MODE
  4595. /*
  4596. * Send an immediate notify ccb to all target mord peripheral
  4597. * drivers affected by this action.
  4598. */
  4599. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4600. if (tstate != NULL) {
  4601. u_int cur_lun;
  4602. u_int max_lun;
  4603. if (lun != CAM_LUN_WILDCARD) {
  4604. cur_lun = 0;
  4605. max_lun = AHD_NUM_LUNS - 1;
  4606. } else {
  4607. cur_lun = lun;
  4608. max_lun = lun;
  4609. }
  4610. for (cur_lun <= max_lun; cur_lun++) {
  4611. struct ahd_tmode_lstate* lstate;
  4612. lstate = tstate->enabled_luns[cur_lun];
  4613. if (lstate == NULL)
  4614. continue;
  4615. ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
  4616. MSG_BUS_DEV_RESET, /*arg*/0);
  4617. ahd_send_lstate_events(ahd, lstate);
  4618. }
  4619. }
  4620. #endif
  4621. /*
  4622. * Go back to async/narrow transfers and renegotiate.
  4623. */
  4624. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4625. AHD_TRANS_CUR, /*paused*/TRUE);
  4626. ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
  4627. /*ppr_options*/0, AHD_TRANS_CUR, /*paused*/TRUE);
  4628. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  4629. lun, AC_SENT_BDR, NULL);
  4630. if (message != NULL
  4631. && (verbose_level <= bootverbose))
  4632. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
  4633. message, devinfo->channel, devinfo->target, found);
  4634. }
  4635. #ifdef AHD_TARGET_MODE
  4636. static void
  4637. ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4638. struct scb *scb)
  4639. {
  4640. /*
  4641. * To facilitate adding multiple messages together,
  4642. * each routine should increment the index and len
  4643. * variables instead of setting them explicitly.
  4644. */
  4645. ahd->msgout_index = 0;
  4646. ahd->msgout_len = 0;
  4647. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  4648. ahd_build_transfer_msg(ahd, devinfo);
  4649. else
  4650. panic("ahd_intr: AWAITING target message with no message");
  4651. ahd->msgout_index = 0;
  4652. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  4653. }
  4654. #endif
  4655. /**************************** Initialization **********************************/
  4656. static u_int
  4657. ahd_sglist_size(struct ahd_softc *ahd)
  4658. {
  4659. bus_size_t list_size;
  4660. list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
  4661. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  4662. list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
  4663. return (list_size);
  4664. }
  4665. /*
  4666. * Calculate the optimum S/G List allocation size. S/G elements used
  4667. * for a given transaction must be physically contiguous. Assume the
  4668. * OS will allocate full pages to us, so it doesn't make sense to request
  4669. * less than a page.
  4670. */
  4671. static u_int
  4672. ahd_sglist_allocsize(struct ahd_softc *ahd)
  4673. {
  4674. bus_size_t sg_list_increment;
  4675. bus_size_t sg_list_size;
  4676. bus_size_t max_list_size;
  4677. bus_size_t best_list_size;
  4678. /* Start out with the minimum required for AHD_NSEG. */
  4679. sg_list_increment = ahd_sglist_size(ahd);
  4680. sg_list_size = sg_list_increment;
  4681. /* Get us as close as possible to a page in size. */
  4682. while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
  4683. sg_list_size += sg_list_increment;
  4684. /*
  4685. * Try to reduce the amount of wastage by allocating
  4686. * multiple pages.
  4687. */
  4688. best_list_size = sg_list_size;
  4689. max_list_size = roundup(sg_list_increment, PAGE_SIZE);
  4690. if (max_list_size < 4 * PAGE_SIZE)
  4691. max_list_size = 4 * PAGE_SIZE;
  4692. if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
  4693. max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
  4694. while ((sg_list_size + sg_list_increment) <= max_list_size
  4695. && (sg_list_size % PAGE_SIZE) != 0) {
  4696. bus_size_t new_mod;
  4697. bus_size_t best_mod;
  4698. sg_list_size += sg_list_increment;
  4699. new_mod = sg_list_size % PAGE_SIZE;
  4700. best_mod = best_list_size % PAGE_SIZE;
  4701. if (new_mod > best_mod || new_mod == 0) {
  4702. best_list_size = sg_list_size;
  4703. }
  4704. }
  4705. return (best_list_size);
  4706. }
  4707. /*
  4708. * Allocate a controller structure for a new device
  4709. * and perform initial initializion.
  4710. */
  4711. struct ahd_softc *
  4712. ahd_alloc(void *platform_arg, char *name)
  4713. {
  4714. struct ahd_softc *ahd;
  4715. #ifndef __FreeBSD__
  4716. ahd = malloc(sizeof(*ahd), M_DEVBUF, M_NOWAIT);
  4717. if (!ahd) {
  4718. printf("aic7xxx: cannot malloc softc!\n");
  4719. free(name, M_DEVBUF);
  4720. return NULL;
  4721. }
  4722. #else
  4723. ahd = device_get_softc((device_t)platform_arg);
  4724. #endif
  4725. memset(ahd, 0, sizeof(*ahd));
  4726. ahd->seep_config = malloc(sizeof(*ahd->seep_config),
  4727. M_DEVBUF, M_NOWAIT);
  4728. if (ahd->seep_config == NULL) {
  4729. #ifndef __FreeBSD__
  4730. free(ahd, M_DEVBUF);
  4731. #endif
  4732. free(name, M_DEVBUF);
  4733. return (NULL);
  4734. }
  4735. LIST_INIT(&ahd->pending_scbs);
  4736. /* We don't know our unit number until the OSM sets it */
  4737. ahd->name = name;
  4738. ahd->unit = -1;
  4739. ahd->description = NULL;
  4740. ahd->bus_description = NULL;
  4741. ahd->channel = 'A';
  4742. ahd->chip = AHD_NONE;
  4743. ahd->features = AHD_FENONE;
  4744. ahd->bugs = AHD_BUGNONE;
  4745. ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A
  4746. | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A;
  4747. ahd_timer_init(&ahd->reset_timer);
  4748. ahd_timer_init(&ahd->stat_timer);
  4749. ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
  4750. ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
  4751. ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
  4752. ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
  4753. ahd->int_coalescing_stop_threshold =
  4754. AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
  4755. if (ahd_platform_alloc(ahd, platform_arg) != 0) {
  4756. ahd_free(ahd);
  4757. ahd = NULL;
  4758. }
  4759. #ifdef AHD_DEBUG
  4760. if ((ahd_debug & AHD_SHOW_MEMORY) != 0) {
  4761. printf("%s: scb size = 0x%x, hscb size = 0x%x\n",
  4762. ahd_name(ahd), (u_int)sizeof(struct scb),
  4763. (u_int)sizeof(struct hardware_scb));
  4764. }
  4765. #endif
  4766. return (ahd);
  4767. }
  4768. int
  4769. ahd_softc_init(struct ahd_softc *ahd)
  4770. {
  4771. ahd->unpause = 0;
  4772. ahd->pause = PAUSE;
  4773. return (0);
  4774. }
  4775. void
  4776. ahd_set_unit(struct ahd_softc *ahd, int unit)
  4777. {
  4778. ahd->unit = unit;
  4779. }
  4780. void
  4781. ahd_set_name(struct ahd_softc *ahd, char *name)
  4782. {
  4783. if (ahd->name != NULL)
  4784. free(ahd->name, M_DEVBUF);
  4785. ahd->name = name;
  4786. }
  4787. void
  4788. ahd_free(struct ahd_softc *ahd)
  4789. {
  4790. int i;
  4791. switch (ahd->init_level) {
  4792. default:
  4793. case 5:
  4794. ahd_shutdown(ahd);
  4795. /* FALLTHROUGH */
  4796. case 4:
  4797. ahd_dmamap_unload(ahd, ahd->shared_data_dmat,
  4798. ahd->shared_data_dmamap);
  4799. /* FALLTHROUGH */
  4800. case 3:
  4801. ahd_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
  4802. ahd->shared_data_dmamap);
  4803. ahd_dmamap_destroy(ahd, ahd->shared_data_dmat,
  4804. ahd->shared_data_dmamap);
  4805. /* FALLTHROUGH */
  4806. case 2:
  4807. ahd_dma_tag_destroy(ahd, ahd->shared_data_dmat);
  4808. case 1:
  4809. #ifndef __linux__
  4810. ahd_dma_tag_destroy(ahd, ahd->buffer_dmat);
  4811. #endif
  4812. break;
  4813. case 0:
  4814. break;
  4815. }
  4816. #ifndef __linux__
  4817. ahd_dma_tag_destroy(ahd, ahd->parent_dmat);
  4818. #endif
  4819. ahd_platform_free(ahd);
  4820. ahd_fini_scbdata(ahd);
  4821. for (i = 0; i < AHD_NUM_TARGETS; i++) {
  4822. struct ahd_tmode_tstate *tstate;
  4823. tstate = ahd->enabled_targets[i];
  4824. if (tstate != NULL) {
  4825. #ifdef AHD_TARGET_MODE
  4826. int j;
  4827. for (j = 0; j < AHD_NUM_LUNS; j++) {
  4828. struct ahd_tmode_lstate *lstate;
  4829. lstate = tstate->enabled_luns[j];
  4830. if (lstate != NULL) {
  4831. xpt_free_path(lstate->path);
  4832. free(lstate, M_DEVBUF);
  4833. }
  4834. }
  4835. #endif
  4836. free(tstate, M_DEVBUF);
  4837. }
  4838. }
  4839. #ifdef AHD_TARGET_MODE
  4840. if (ahd->black_hole != NULL) {
  4841. xpt_free_path(ahd->black_hole->path);
  4842. free(ahd->black_hole, M_DEVBUF);
  4843. }
  4844. #endif
  4845. if (ahd->name != NULL)
  4846. free(ahd->name, M_DEVBUF);
  4847. if (ahd->seep_config != NULL)
  4848. free(ahd->seep_config, M_DEVBUF);
  4849. if (ahd->saved_stack != NULL)
  4850. free(ahd->saved_stack, M_DEVBUF);
  4851. #ifndef __FreeBSD__
  4852. free(ahd, M_DEVBUF);
  4853. #endif
  4854. return;
  4855. }
  4856. void
  4857. ahd_shutdown(void *arg)
  4858. {
  4859. struct ahd_softc *ahd;
  4860. ahd = (struct ahd_softc *)arg;
  4861. /*
  4862. * Stop periodic timer callbacks.
  4863. */
  4864. ahd_timer_stop(&ahd->reset_timer);
  4865. ahd_timer_stop(&ahd->stat_timer);
  4866. /* This will reset most registers to 0, but not all */
  4867. ahd_reset(ahd, /*reinit*/FALSE);
  4868. }
  4869. /*
  4870. * Reset the controller and record some information about it
  4871. * that is only available just after a reset. If "reinit" is
  4872. * non-zero, this reset occured after initial configuration
  4873. * and the caller requests that the chip be fully reinitialized
  4874. * to a runable state. Chip interrupts are *not* enabled after
  4875. * a reinitialization. The caller must enable interrupts via
  4876. * ahd_intr_enable().
  4877. */
  4878. int
  4879. ahd_reset(struct ahd_softc *ahd, int reinit)
  4880. {
  4881. u_int sxfrctl1;
  4882. int wait;
  4883. uint32_t cmd;
  4884. /*
  4885. * Preserve the value of the SXFRCTL1 register for all channels.
  4886. * It contains settings that affect termination and we don't want
  4887. * to disturb the integrity of the bus.
  4888. */
  4889. ahd_pause(ahd);
  4890. ahd_update_modes(ahd);
  4891. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4892. sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
  4893. cmd = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
  4894. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  4895. uint32_t mod_cmd;
  4896. /*
  4897. * A4 Razor #632
  4898. * During the assertion of CHIPRST, the chip
  4899. * does not disable its parity logic prior to
  4900. * the start of the reset. This may cause a
  4901. * parity error to be detected and thus a
  4902. * spurious SERR or PERR assertion. Disble
  4903. * PERR and SERR responses during the CHIPRST.
  4904. */
  4905. mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN);
  4906. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  4907. mod_cmd, /*bytes*/2);
  4908. }
  4909. ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
  4910. /*
  4911. * Ensure that the reset has finished. We delay 1000us
  4912. * prior to reading the register to make sure the chip
  4913. * has sufficiently completed its reset to handle register
  4914. * accesses.
  4915. */
  4916. wait = 1000;
  4917. do {
  4918. ahd_delay(1000);
  4919. } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
  4920. if (wait == 0) {
  4921. printf("%s: WARNING - Failed chip reset! "
  4922. "Trying to initialize anyway.\n", ahd_name(ahd));
  4923. }
  4924. ahd_outb(ahd, HCNTRL, ahd->pause);
  4925. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  4926. /*
  4927. * Clear any latched PCI error status and restore
  4928. * previous SERR and PERR response enables.
  4929. */
  4930. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  4931. 0xFF, /*bytes*/1);
  4932. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  4933. cmd, /*bytes*/2);
  4934. }
  4935. /*
  4936. * Mode should be SCSI after a chip reset, but lets
  4937. * set it just to be safe. We touch the MODE_PTR
  4938. * register directly so as to bypass the lazy update
  4939. * code in ahd_set_modes().
  4940. */
  4941. ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4942. ahd_outb(ahd, MODE_PTR,
  4943. ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
  4944. /*
  4945. * Restore SXFRCTL1.
  4946. *
  4947. * We must always initialize STPWEN to 1 before we
  4948. * restore the saved values. STPWEN is initialized
  4949. * to a tri-state condition which can only be cleared
  4950. * by turning it on.
  4951. */
  4952. ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
  4953. ahd_outb(ahd, SXFRCTL1, sxfrctl1);
  4954. /* Determine chip configuration */
  4955. ahd->features &= ~AHD_WIDE;
  4956. if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
  4957. ahd->features |= AHD_WIDE;
  4958. /*
  4959. * If a recovery action has forced a chip reset,
  4960. * re-initialize the chip to our liking.
  4961. */
  4962. if (reinit != 0)
  4963. ahd_chip_init(ahd);
  4964. return (0);
  4965. }
  4966. /*
  4967. * Determine the number of SCBs available on the controller
  4968. */
  4969. int
  4970. ahd_probe_scbs(struct ahd_softc *ahd) {
  4971. int i;
  4972. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  4973. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  4974. for (i = 0; i < AHD_SCB_MAX; i++) {
  4975. int j;
  4976. ahd_set_scbptr(ahd, i);
  4977. ahd_outw(ahd, SCB_BASE, i);
  4978. for (j = 2; j < 64; j++)
  4979. ahd_outb(ahd, SCB_BASE+j, 0);
  4980. /* Start out life as unallocated (needing an abort) */
  4981. ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
  4982. if (ahd_inw_scbram(ahd, SCB_BASE) != i)
  4983. break;
  4984. ahd_set_scbptr(ahd, 0);
  4985. if (ahd_inw_scbram(ahd, SCB_BASE) != 0)
  4986. break;
  4987. }
  4988. return (i);
  4989. }
  4990. static void
  4991. ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  4992. {
  4993. dma_addr_t *baddr;
  4994. baddr = (dma_addr_t *)arg;
  4995. *baddr = segs->ds_addr;
  4996. }
  4997. static void
  4998. ahd_initialize_hscbs(struct ahd_softc *ahd)
  4999. {
  5000. int i;
  5001. for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
  5002. ahd_set_scbptr(ahd, i);
  5003. /* Clear the control byte. */
  5004. ahd_outb(ahd, SCB_CONTROL, 0);
  5005. /* Set the next pointer */
  5006. ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
  5007. }
  5008. }
  5009. static int
  5010. ahd_init_scbdata(struct ahd_softc *ahd)
  5011. {
  5012. struct scb_data *scb_data;
  5013. int i;
  5014. scb_data = &ahd->scb_data;
  5015. TAILQ_INIT(&scb_data->free_scbs);
  5016. for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
  5017. LIST_INIT(&scb_data->free_scb_lists[i]);
  5018. LIST_INIT(&scb_data->any_dev_free_scb_list);
  5019. SLIST_INIT(&scb_data->hscb_maps);
  5020. SLIST_INIT(&scb_data->sg_maps);
  5021. SLIST_INIT(&scb_data->sense_maps);
  5022. /* Determine the number of hardware SCBs and initialize them */
  5023. scb_data->maxhscbs = ahd_probe_scbs(ahd);
  5024. if (scb_data->maxhscbs == 0) {
  5025. printf("%s: No SCB space found\n", ahd_name(ahd));
  5026. return (ENXIO);
  5027. }
  5028. ahd_initialize_hscbs(ahd);
  5029. /*
  5030. * Create our DMA tags. These tags define the kinds of device
  5031. * accessible memory allocations and memory mappings we will
  5032. * need to perform during normal operation.
  5033. *
  5034. * Unless we need to further restrict the allocation, we rely
  5035. * on the restrictions of the parent dmat, hence the common
  5036. * use of MAXADDR and MAXSIZE.
  5037. */
  5038. /* DMA tag for our hardware scb structures */
  5039. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5040. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5041. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5042. /*highaddr*/BUS_SPACE_MAXADDR,
  5043. /*filter*/NULL, /*filterarg*/NULL,
  5044. PAGE_SIZE, /*nsegments*/1,
  5045. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5046. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  5047. goto error_exit;
  5048. }
  5049. scb_data->init_level++;
  5050. /* DMA tag for our S/G structures. */
  5051. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8,
  5052. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5053. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5054. /*highaddr*/BUS_SPACE_MAXADDR,
  5055. /*filter*/NULL, /*filterarg*/NULL,
  5056. ahd_sglist_allocsize(ahd), /*nsegments*/1,
  5057. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5058. /*flags*/0, &scb_data->sg_dmat) != 0) {
  5059. goto error_exit;
  5060. }
  5061. #ifdef AHD_DEBUG
  5062. if ((ahd_debug & AHD_SHOW_MEMORY) != 0)
  5063. printf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd),
  5064. ahd_sglist_allocsize(ahd));
  5065. #endif
  5066. scb_data->init_level++;
  5067. /* DMA tag for our sense buffers. We allocate in page sized chunks */
  5068. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5069. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5070. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5071. /*highaddr*/BUS_SPACE_MAXADDR,
  5072. /*filter*/NULL, /*filterarg*/NULL,
  5073. PAGE_SIZE, /*nsegments*/1,
  5074. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5075. /*flags*/0, &scb_data->sense_dmat) != 0) {
  5076. goto error_exit;
  5077. }
  5078. scb_data->init_level++;
  5079. /* Perform initial CCB allocation */
  5080. ahd_alloc_scbs(ahd);
  5081. if (scb_data->numscbs == 0) {
  5082. printf("%s: ahd_init_scbdata - "
  5083. "Unable to allocate initial scbs\n",
  5084. ahd_name(ahd));
  5085. goto error_exit;
  5086. }
  5087. /*
  5088. * Note that we were successfull
  5089. */
  5090. return (0);
  5091. error_exit:
  5092. return (ENOMEM);
  5093. }
  5094. static struct scb *
  5095. ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
  5096. {
  5097. struct scb *scb;
  5098. /*
  5099. * Look on the pending list.
  5100. */
  5101. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  5102. if (SCB_GET_TAG(scb) == tag)
  5103. return (scb);
  5104. }
  5105. /*
  5106. * Then on all of the collision free lists.
  5107. */
  5108. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5109. struct scb *list_scb;
  5110. list_scb = scb;
  5111. do {
  5112. if (SCB_GET_TAG(list_scb) == tag)
  5113. return (list_scb);
  5114. list_scb = LIST_NEXT(list_scb, collision_links);
  5115. } while (list_scb);
  5116. }
  5117. /*
  5118. * And finally on the generic free list.
  5119. */
  5120. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  5121. if (SCB_GET_TAG(scb) == tag)
  5122. return (scb);
  5123. }
  5124. return (NULL);
  5125. }
  5126. static void
  5127. ahd_fini_scbdata(struct ahd_softc *ahd)
  5128. {
  5129. struct scb_data *scb_data;
  5130. scb_data = &ahd->scb_data;
  5131. if (scb_data == NULL)
  5132. return;
  5133. switch (scb_data->init_level) {
  5134. default:
  5135. case 7:
  5136. {
  5137. struct map_node *sns_map;
  5138. while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
  5139. SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
  5140. ahd_dmamap_unload(ahd, scb_data->sense_dmat,
  5141. sns_map->dmamap);
  5142. ahd_dmamem_free(ahd, scb_data->sense_dmat,
  5143. sns_map->vaddr, sns_map->dmamap);
  5144. free(sns_map, M_DEVBUF);
  5145. }
  5146. ahd_dma_tag_destroy(ahd, scb_data->sense_dmat);
  5147. /* FALLTHROUGH */
  5148. }
  5149. case 6:
  5150. {
  5151. struct map_node *sg_map;
  5152. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
  5153. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  5154. ahd_dmamap_unload(ahd, scb_data->sg_dmat,
  5155. sg_map->dmamap);
  5156. ahd_dmamem_free(ahd, scb_data->sg_dmat,
  5157. sg_map->vaddr, sg_map->dmamap);
  5158. free(sg_map, M_DEVBUF);
  5159. }
  5160. ahd_dma_tag_destroy(ahd, scb_data->sg_dmat);
  5161. /* FALLTHROUGH */
  5162. }
  5163. case 5:
  5164. {
  5165. struct map_node *hscb_map;
  5166. while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
  5167. SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
  5168. ahd_dmamap_unload(ahd, scb_data->hscb_dmat,
  5169. hscb_map->dmamap);
  5170. ahd_dmamem_free(ahd, scb_data->hscb_dmat,
  5171. hscb_map->vaddr, hscb_map->dmamap);
  5172. free(hscb_map, M_DEVBUF);
  5173. }
  5174. ahd_dma_tag_destroy(ahd, scb_data->hscb_dmat);
  5175. /* FALLTHROUGH */
  5176. }
  5177. case 4:
  5178. case 3:
  5179. case 2:
  5180. case 1:
  5181. case 0:
  5182. break;
  5183. }
  5184. }
  5185. /*
  5186. * DSP filter Bypass must be enabled until the first selection
  5187. * after a change in bus mode (Razor #491 and #493).
  5188. */
  5189. static void
  5190. ahd_setup_iocell_workaround(struct ahd_softc *ahd)
  5191. {
  5192. ahd_mode_state saved_modes;
  5193. saved_modes = ahd_save_modes(ahd);
  5194. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5195. ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
  5196. | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
  5197. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
  5198. #ifdef AHD_DEBUG
  5199. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5200. printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
  5201. #endif
  5202. ahd_restore_modes(ahd, saved_modes);
  5203. ahd->flags &= ~AHD_HAD_FIRST_SEL;
  5204. }
  5205. static void
  5206. ahd_iocell_first_selection(struct ahd_softc *ahd)
  5207. {
  5208. ahd_mode_state saved_modes;
  5209. u_int sblkctl;
  5210. if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0)
  5211. return;
  5212. saved_modes = ahd_save_modes(ahd);
  5213. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5214. sblkctl = ahd_inb(ahd, SBLKCTL);
  5215. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5216. #ifdef AHD_DEBUG
  5217. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5218. printf("%s: iocell first selection\n", ahd_name(ahd));
  5219. #endif
  5220. if ((sblkctl & ENAB40) != 0) {
  5221. ahd_outb(ahd, DSPDATACTL,
  5222. ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
  5223. #ifdef AHD_DEBUG
  5224. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5225. printf("%s: BYPASS now disabled\n", ahd_name(ahd));
  5226. #endif
  5227. }
  5228. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
  5229. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5230. ahd_restore_modes(ahd, saved_modes);
  5231. ahd->flags |= AHD_HAD_FIRST_SEL;
  5232. }
  5233. /*************************** SCB Management ***********************************/
  5234. static void
  5235. ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
  5236. {
  5237. struct scb_list *free_list;
  5238. struct scb_tailq *free_tailq;
  5239. struct scb *first_scb;
  5240. scb->flags |= SCB_ON_COL_LIST;
  5241. AHD_SET_SCB_COL_IDX(scb, col_idx);
  5242. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5243. free_tailq = &ahd->scb_data.free_scbs;
  5244. first_scb = LIST_FIRST(free_list);
  5245. if (first_scb != NULL) {
  5246. LIST_INSERT_AFTER(first_scb, scb, collision_links);
  5247. } else {
  5248. LIST_INSERT_HEAD(free_list, scb, collision_links);
  5249. TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
  5250. }
  5251. }
  5252. static void
  5253. ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
  5254. {
  5255. struct scb_list *free_list;
  5256. struct scb_tailq *free_tailq;
  5257. struct scb *first_scb;
  5258. u_int col_idx;
  5259. scb->flags &= ~SCB_ON_COL_LIST;
  5260. col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
  5261. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5262. free_tailq = &ahd->scb_data.free_scbs;
  5263. first_scb = LIST_FIRST(free_list);
  5264. if (first_scb == scb) {
  5265. struct scb *next_scb;
  5266. /*
  5267. * Maintain order in the collision free
  5268. * lists for fairness if this device has
  5269. * other colliding tags active.
  5270. */
  5271. next_scb = LIST_NEXT(scb, collision_links);
  5272. if (next_scb != NULL) {
  5273. TAILQ_INSERT_AFTER(free_tailq, scb,
  5274. next_scb, links.tqe);
  5275. }
  5276. TAILQ_REMOVE(free_tailq, scb, links.tqe);
  5277. }
  5278. LIST_REMOVE(scb, collision_links);
  5279. }
  5280. /*
  5281. * Get a free scb. If there are none, see if we can allocate a new SCB.
  5282. */
  5283. struct scb *
  5284. ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
  5285. {
  5286. struct scb *scb;
  5287. int tries;
  5288. tries = 0;
  5289. look_again:
  5290. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5291. if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
  5292. ahd_rem_col_list(ahd, scb);
  5293. goto found;
  5294. }
  5295. }
  5296. if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
  5297. if (tries++ != 0)
  5298. return (NULL);
  5299. ahd_alloc_scbs(ahd);
  5300. goto look_again;
  5301. }
  5302. LIST_REMOVE(scb, links.le);
  5303. if (col_idx != AHD_NEVER_COL_IDX
  5304. && (scb->col_scb != NULL)
  5305. && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
  5306. LIST_REMOVE(scb->col_scb, links.le);
  5307. ahd_add_col_list(ahd, scb->col_scb, col_idx);
  5308. }
  5309. found:
  5310. scb->flags |= SCB_ACTIVE;
  5311. return (scb);
  5312. }
  5313. /*
  5314. * Return an SCB resource to the free list.
  5315. */
  5316. void
  5317. ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
  5318. {
  5319. /* Clean up for the next user */
  5320. scb->flags = SCB_FLAG_NONE;
  5321. scb->hscb->control = 0;
  5322. ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
  5323. if (scb->col_scb == NULL) {
  5324. /*
  5325. * No collision possible. Just free normally.
  5326. */
  5327. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5328. scb, links.le);
  5329. } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
  5330. /*
  5331. * The SCB we might have collided with is on
  5332. * a free collision list. Put both SCBs on
  5333. * the generic list.
  5334. */
  5335. ahd_rem_col_list(ahd, scb->col_scb);
  5336. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5337. scb, links.le);
  5338. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5339. scb->col_scb, links.le);
  5340. } else if ((scb->col_scb->flags
  5341. & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
  5342. && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
  5343. /*
  5344. * The SCB we might collide with on the next allocation
  5345. * is still active in a non-packetized, tagged, context.
  5346. * Put us on the SCB collision list.
  5347. */
  5348. ahd_add_col_list(ahd, scb,
  5349. AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
  5350. } else {
  5351. /*
  5352. * The SCB we might collide with on the next allocation
  5353. * is either active in a packetized context, or free.
  5354. * Since we can't collide, put this SCB on the generic
  5355. * free list.
  5356. */
  5357. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5358. scb, links.le);
  5359. }
  5360. ahd_platform_scb_free(ahd, scb);
  5361. }
  5362. void
  5363. ahd_alloc_scbs(struct ahd_softc *ahd)
  5364. {
  5365. struct scb_data *scb_data;
  5366. struct scb *next_scb;
  5367. struct hardware_scb *hscb;
  5368. struct map_node *hscb_map;
  5369. struct map_node *sg_map;
  5370. struct map_node *sense_map;
  5371. uint8_t *segs;
  5372. uint8_t *sense_data;
  5373. dma_addr_t hscb_busaddr;
  5374. dma_addr_t sg_busaddr;
  5375. dma_addr_t sense_busaddr;
  5376. int newcount;
  5377. int i;
  5378. scb_data = &ahd->scb_data;
  5379. if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
  5380. /* Can't allocate any more */
  5381. return;
  5382. if (scb_data->scbs_left != 0) {
  5383. int offset;
  5384. offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
  5385. hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
  5386. hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
  5387. hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
  5388. } else {
  5389. hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
  5390. if (hscb_map == NULL)
  5391. return;
  5392. /* Allocate the next batch of hardware SCBs */
  5393. if (ahd_dmamem_alloc(ahd, scb_data->hscb_dmat,
  5394. (void **)&hscb_map->vaddr,
  5395. BUS_DMA_NOWAIT, &hscb_map->dmamap) != 0) {
  5396. free(hscb_map, M_DEVBUF);
  5397. return;
  5398. }
  5399. SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
  5400. ahd_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap,
  5401. hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5402. &hscb_map->physaddr, /*flags*/0);
  5403. hscb = (struct hardware_scb *)hscb_map->vaddr;
  5404. hscb_busaddr = hscb_map->physaddr;
  5405. scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
  5406. }
  5407. if (scb_data->sgs_left != 0) {
  5408. int offset;
  5409. offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
  5410. - scb_data->sgs_left) * ahd_sglist_size(ahd);
  5411. sg_map = SLIST_FIRST(&scb_data->sg_maps);
  5412. segs = sg_map->vaddr + offset;
  5413. sg_busaddr = sg_map->physaddr + offset;
  5414. } else {
  5415. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  5416. if (sg_map == NULL)
  5417. return;
  5418. /* Allocate the next batch of S/G lists */
  5419. if (ahd_dmamem_alloc(ahd, scb_data->sg_dmat,
  5420. (void **)&sg_map->vaddr,
  5421. BUS_DMA_NOWAIT, &sg_map->dmamap) != 0) {
  5422. free(sg_map, M_DEVBUF);
  5423. return;
  5424. }
  5425. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  5426. ahd_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap,
  5427. sg_map->vaddr, ahd_sglist_allocsize(ahd),
  5428. ahd_dmamap_cb, &sg_map->physaddr, /*flags*/0);
  5429. segs = sg_map->vaddr;
  5430. sg_busaddr = sg_map->physaddr;
  5431. scb_data->sgs_left =
  5432. ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
  5433. #ifdef AHD_DEBUG
  5434. if (ahd_debug & AHD_SHOW_MEMORY)
  5435. printf("Mapped SG data\n");
  5436. #endif
  5437. }
  5438. if (scb_data->sense_left != 0) {
  5439. int offset;
  5440. offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
  5441. sense_map = SLIST_FIRST(&scb_data->sense_maps);
  5442. sense_data = sense_map->vaddr + offset;
  5443. sense_busaddr = sense_map->physaddr + offset;
  5444. } else {
  5445. sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
  5446. if (sense_map == NULL)
  5447. return;
  5448. /* Allocate the next batch of sense buffers */
  5449. if (ahd_dmamem_alloc(ahd, scb_data->sense_dmat,
  5450. (void **)&sense_map->vaddr,
  5451. BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) {
  5452. free(sense_map, M_DEVBUF);
  5453. return;
  5454. }
  5455. SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
  5456. ahd_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap,
  5457. sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5458. &sense_map->physaddr, /*flags*/0);
  5459. sense_data = sense_map->vaddr;
  5460. sense_busaddr = sense_map->physaddr;
  5461. scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
  5462. #ifdef AHD_DEBUG
  5463. if (ahd_debug & AHD_SHOW_MEMORY)
  5464. printf("Mapped sense data\n");
  5465. #endif
  5466. }
  5467. newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
  5468. newcount = MIN(newcount, scb_data->sgs_left);
  5469. newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
  5470. scb_data->sense_left -= newcount;
  5471. scb_data->scbs_left -= newcount;
  5472. scb_data->sgs_left -= newcount;
  5473. for (i = 0; i < newcount; i++) {
  5474. u_int col_tag;
  5475. struct scb_platform_data *pdata;
  5476. #ifndef __linux__
  5477. int error;
  5478. #endif
  5479. next_scb = (struct scb *)malloc(sizeof(*next_scb),
  5480. M_DEVBUF, M_NOWAIT);
  5481. if (next_scb == NULL)
  5482. break;
  5483. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  5484. M_DEVBUF, M_NOWAIT);
  5485. if (pdata == NULL) {
  5486. free(next_scb, M_DEVBUF);
  5487. break;
  5488. }
  5489. next_scb->platform_data = pdata;
  5490. next_scb->hscb_map = hscb_map;
  5491. next_scb->sg_map = sg_map;
  5492. next_scb->sense_map = sense_map;
  5493. next_scb->sg_list = segs;
  5494. next_scb->sense_data = sense_data;
  5495. next_scb->sense_busaddr = sense_busaddr;
  5496. memset(hscb, 0, sizeof(*hscb));
  5497. next_scb->hscb = hscb;
  5498. hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
  5499. /*
  5500. * The sequencer always starts with the second entry.
  5501. * The first entry is embedded in the scb.
  5502. */
  5503. next_scb->sg_list_busaddr = sg_busaddr;
  5504. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  5505. next_scb->sg_list_busaddr
  5506. += sizeof(struct ahd_dma64_seg);
  5507. else
  5508. next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
  5509. next_scb->ahd_softc = ahd;
  5510. next_scb->flags = SCB_FLAG_NONE;
  5511. #ifndef __linux__
  5512. error = ahd_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0,
  5513. &next_scb->dmamap);
  5514. if (error != 0) {
  5515. free(next_scb, M_DEVBUF);
  5516. free(pdata, M_DEVBUF);
  5517. break;
  5518. }
  5519. #endif
  5520. next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
  5521. col_tag = scb_data->numscbs ^ 0x100;
  5522. next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
  5523. if (next_scb->col_scb != NULL)
  5524. next_scb->col_scb->col_scb = next_scb;
  5525. ahd_free_scb(ahd, next_scb);
  5526. hscb++;
  5527. hscb_busaddr += sizeof(*hscb);
  5528. segs += ahd_sglist_size(ahd);
  5529. sg_busaddr += ahd_sglist_size(ahd);
  5530. sense_data += AHD_SENSE_BUFSIZE;
  5531. sense_busaddr += AHD_SENSE_BUFSIZE;
  5532. scb_data->numscbs++;
  5533. }
  5534. }
  5535. void
  5536. ahd_controller_info(struct ahd_softc *ahd, char *buf)
  5537. {
  5538. const char *speed;
  5539. const char *type;
  5540. int len;
  5541. len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
  5542. buf += len;
  5543. speed = "Ultra320 ";
  5544. if ((ahd->features & AHD_WIDE) != 0) {
  5545. type = "Wide ";
  5546. } else {
  5547. type = "Single ";
  5548. }
  5549. len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
  5550. speed, type, ahd->channel, ahd->our_id);
  5551. buf += len;
  5552. sprintf(buf, "%s, %d SCBs", ahd->bus_description,
  5553. ahd->scb_data.maxhscbs);
  5554. }
  5555. static const char *channel_strings[] = {
  5556. "Primary Low",
  5557. "Primary High",
  5558. "Secondary Low",
  5559. "Secondary High"
  5560. };
  5561. static const char *termstat_strings[] = {
  5562. "Terminated Correctly",
  5563. "Over Terminated",
  5564. "Under Terminated",
  5565. "Not Configured"
  5566. };
  5567. /*
  5568. * Start the board, ready for normal operation
  5569. */
  5570. int
  5571. ahd_init(struct ahd_softc *ahd)
  5572. {
  5573. uint8_t *base_vaddr;
  5574. uint8_t *next_vaddr;
  5575. dma_addr_t next_baddr;
  5576. size_t driver_data_size;
  5577. int i;
  5578. int error;
  5579. u_int warn_user;
  5580. uint8_t current_sensing;
  5581. uint8_t fstat;
  5582. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5583. ahd->stack_size = ahd_probe_stack_size(ahd);
  5584. ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
  5585. M_DEVBUF, M_NOWAIT);
  5586. if (ahd->saved_stack == NULL)
  5587. return (ENOMEM);
  5588. /*
  5589. * Verify that the compiler hasn't over-agressively
  5590. * padded important structures.
  5591. */
  5592. if (sizeof(struct hardware_scb) != 64)
  5593. panic("Hardware SCB size is incorrect");
  5594. #ifdef AHD_DEBUG
  5595. if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
  5596. ahd->flags |= AHD_SEQUENCER_DEBUG;
  5597. #endif
  5598. /*
  5599. * Default to allowing initiator operations.
  5600. */
  5601. ahd->flags |= AHD_INITIATORROLE;
  5602. /*
  5603. * Only allow target mode features if this unit has them enabled.
  5604. */
  5605. if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
  5606. ahd->features &= ~AHD_TARGETMODE;
  5607. #ifndef __linux__
  5608. /* DMA tag for mapping buffers into device visible space. */
  5609. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5610. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5611. /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING
  5612. ? (dma_addr_t)0x7FFFFFFFFFULL
  5613. : BUS_SPACE_MAXADDR_32BIT,
  5614. /*highaddr*/BUS_SPACE_MAXADDR,
  5615. /*filter*/NULL, /*filterarg*/NULL,
  5616. /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE,
  5617. /*nsegments*/AHD_NSEG,
  5618. /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
  5619. /*flags*/BUS_DMA_ALLOCNOW,
  5620. &ahd->buffer_dmat) != 0) {
  5621. return (ENOMEM);
  5622. }
  5623. #endif
  5624. ahd->init_level++;
  5625. /*
  5626. * DMA tag for our command fifos and other data in system memory
  5627. * the card's sequencer must be able to access. For initiator
  5628. * roles, we need to allocate space for the qoutfifo. When providing
  5629. * for the target mode role, we must additionally provide space for
  5630. * the incoming target command fifo.
  5631. */
  5632. driver_data_size = AHD_SCB_MAX * sizeof(uint16_t)
  5633. + sizeof(struct hardware_scb);
  5634. if ((ahd->features & AHD_TARGETMODE) != 0)
  5635. driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5636. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
  5637. driver_data_size += PKT_OVERRUN_BUFSIZE;
  5638. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5639. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5640. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5641. /*highaddr*/BUS_SPACE_MAXADDR,
  5642. /*filter*/NULL, /*filterarg*/NULL,
  5643. driver_data_size,
  5644. /*nsegments*/1,
  5645. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5646. /*flags*/0, &ahd->shared_data_dmat) != 0) {
  5647. return (ENOMEM);
  5648. }
  5649. ahd->init_level++;
  5650. /* Allocation of driver data */
  5651. if (ahd_dmamem_alloc(ahd, ahd->shared_data_dmat,
  5652. (void **)&base_vaddr,
  5653. BUS_DMA_NOWAIT, &ahd->shared_data_dmamap) != 0) {
  5654. return (ENOMEM);
  5655. }
  5656. ahd->init_level++;
  5657. /* And permanently map it in */
  5658. ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_dmamap,
  5659. base_vaddr, driver_data_size, ahd_dmamap_cb,
  5660. &ahd->shared_data_busaddr, /*flags*/0);
  5661. ahd->qoutfifo = (uint16_t *)base_vaddr;
  5662. next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
  5663. next_baddr = ahd->shared_data_busaddr + AHD_QOUT_SIZE*sizeof(uint16_t);
  5664. if ((ahd->features & AHD_TARGETMODE) != 0) {
  5665. ahd->targetcmds = (struct target_cmd *)next_vaddr;
  5666. next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5667. next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5668. }
  5669. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
  5670. ahd->overrun_buf = next_vaddr;
  5671. next_vaddr += PKT_OVERRUN_BUFSIZE;
  5672. next_baddr += PKT_OVERRUN_BUFSIZE;
  5673. }
  5674. /*
  5675. * We need one SCB to serve as the "next SCB". Since the
  5676. * tag identifier in this SCB will never be used, there is
  5677. * no point in using a valid HSCB tag from an SCB pulled from
  5678. * the standard free pool. So, we allocate this "sentinel"
  5679. * specially from the DMA safe memory chunk used for the QOUTFIFO.
  5680. */
  5681. ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
  5682. ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
  5683. ahd->init_level++;
  5684. /* Allocate SCB data now that buffer_dmat is initialized */
  5685. if (ahd_init_scbdata(ahd) != 0)
  5686. return (ENOMEM);
  5687. if ((ahd->flags & AHD_INITIATORROLE) == 0)
  5688. ahd->flags &= ~AHD_RESET_BUS_A;
  5689. /*
  5690. * Before committing these settings to the chip, give
  5691. * the OSM one last chance to modify our configuration.
  5692. */
  5693. ahd_platform_init(ahd);
  5694. /* Bring up the chip. */
  5695. ahd_chip_init(ahd);
  5696. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5697. if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
  5698. goto init_done;
  5699. /*
  5700. * Verify termination based on current draw and
  5701. * warn user if the bus is over/under terminated.
  5702. */
  5703. error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
  5704. CURSENSE_ENB);
  5705. if (error != 0) {
  5706. printf("%s: current sensing timeout 1\n", ahd_name(ahd));
  5707. goto init_done;
  5708. }
  5709. for (i = 20, fstat = FLX_FSTAT_BUSY;
  5710. (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
  5711. error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
  5712. if (error != 0) {
  5713. printf("%s: current sensing timeout 2\n",
  5714. ahd_name(ahd));
  5715. goto init_done;
  5716. }
  5717. }
  5718. if (i == 0) {
  5719. printf("%s: Timedout during current-sensing test\n",
  5720. ahd_name(ahd));
  5721. goto init_done;
  5722. }
  5723. /* Latch Current Sensing status. */
  5724. error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, &current_sensing);
  5725. if (error != 0) {
  5726. printf("%s: current sensing timeout 3\n", ahd_name(ahd));
  5727. goto init_done;
  5728. }
  5729. /* Diable current sensing. */
  5730. ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
  5731. #ifdef AHD_DEBUG
  5732. if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
  5733. printf("%s: current_sensing == 0x%x\n",
  5734. ahd_name(ahd), current_sensing);
  5735. }
  5736. #endif
  5737. warn_user = 0;
  5738. for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
  5739. u_int term_stat;
  5740. term_stat = (current_sensing & FLX_CSTAT_MASK);
  5741. switch (term_stat) {
  5742. case FLX_CSTAT_OVER:
  5743. case FLX_CSTAT_UNDER:
  5744. warn_user++;
  5745. case FLX_CSTAT_INVALID:
  5746. case FLX_CSTAT_OKAY:
  5747. if (warn_user == 0 && bootverbose == 0)
  5748. break;
  5749. printf("%s: %s Channel %s\n", ahd_name(ahd),
  5750. channel_strings[i], termstat_strings[term_stat]);
  5751. break;
  5752. }
  5753. }
  5754. if (warn_user) {
  5755. printf("%s: WARNING. Termination is not configured correctly.\n"
  5756. "%s: WARNING. SCSI bus operations may FAIL.\n",
  5757. ahd_name(ahd), ahd_name(ahd));
  5758. }
  5759. init_done:
  5760. ahd_restart(ahd);
  5761. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  5762. ahd_stat_timer, ahd);
  5763. return (0);
  5764. }
  5765. /*
  5766. * (Re)initialize chip state after a chip reset.
  5767. */
  5768. static void
  5769. ahd_chip_init(struct ahd_softc *ahd)
  5770. {
  5771. uint32_t busaddr;
  5772. u_int sxfrctl1;
  5773. u_int scsiseq_template;
  5774. u_int wait;
  5775. u_int i;
  5776. u_int target;
  5777. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5778. /*
  5779. * Take the LED out of diagnostic mode
  5780. */
  5781. ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
  5782. /*
  5783. * Return HS_MAILBOX to its default value.
  5784. */
  5785. ahd->hs_mailbox = 0;
  5786. ahd_outb(ahd, HS_MAILBOX, 0);
  5787. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
  5788. ahd_outb(ahd, IOWNID, ahd->our_id);
  5789. ahd_outb(ahd, TOWNID, ahd->our_id);
  5790. sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
  5791. sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
  5792. if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
  5793. && (ahd->seltime != STIMESEL_MIN)) {
  5794. /*
  5795. * The selection timer duration is twice as long
  5796. * as it should be. Halve it by adding "1" to
  5797. * the user specified setting.
  5798. */
  5799. sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
  5800. } else {
  5801. sxfrctl1 |= ahd->seltime;
  5802. }
  5803. ahd_outb(ahd, SXFRCTL0, DFON);
  5804. ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
  5805. ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  5806. /*
  5807. * Now that termination is set, wait for up
  5808. * to 500ms for our transceivers to settle. If
  5809. * the adapter does not have a cable attached,
  5810. * the transceivers may never settle, so don't
  5811. * complain if we fail here.
  5812. */
  5813. for (wait = 10000;
  5814. (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  5815. wait--)
  5816. ahd_delay(100);
  5817. /* Clear any false bus resets due to the transceivers settling */
  5818. ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
  5819. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5820. /* Initialize mode specific S/G state. */
  5821. for (i = 0; i < 2; i++) {
  5822. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  5823. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  5824. ahd_outb(ahd, SG_STATE, 0);
  5825. ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
  5826. ahd_outb(ahd, SEQIMODE,
  5827. ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
  5828. |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
  5829. }
  5830. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5831. ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
  5832. ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
  5833. ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
  5834. ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
  5835. if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
  5836. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
  5837. } else {
  5838. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
  5839. }
  5840. ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
  5841. if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
  5842. /*
  5843. * Do not issue a target abort when a split completion
  5844. * error occurs. Let our PCIX interrupt handler deal
  5845. * with it instead. H2A4 Razor #625
  5846. */
  5847. ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
  5848. if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
  5849. ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
  5850. /*
  5851. * Tweak IOCELL settings.
  5852. */
  5853. if ((ahd->flags & AHD_HP_BOARD) != 0) {
  5854. for (i = 0; i < NUMDSPS; i++) {
  5855. ahd_outb(ahd, DSPSELECT, i);
  5856. ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
  5857. }
  5858. #ifdef AHD_DEBUG
  5859. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5860. printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
  5861. WRTBIASCTL_HP_DEFAULT);
  5862. #endif
  5863. }
  5864. ahd_setup_iocell_workaround(ahd);
  5865. /*
  5866. * Enable LQI Manager interrupts.
  5867. */
  5868. ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
  5869. | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
  5870. | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
  5871. ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
  5872. /*
  5873. * An interrupt from LQOBUSFREE is made redundant by the
  5874. * BUSFREE interrupt. We choose to have the sequencer catch
  5875. * LQOPHCHGINPKT errors manually for the command phase at the
  5876. * start of a packetized selection case.
  5877. ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE|ENLQOPHACHGINPKT);
  5878. */
  5879. ahd_outb(ahd, LQOMODE1, 0);
  5880. /*
  5881. * Setup sequencer interrupt handlers.
  5882. */
  5883. ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
  5884. ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
  5885. /*
  5886. * Setup SCB Offset registers.
  5887. */
  5888. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  5889. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
  5890. pkt_long_lun));
  5891. } else {
  5892. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
  5893. }
  5894. ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
  5895. ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
  5896. ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
  5897. ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
  5898. shared_data.idata.cdb));
  5899. ahd_outb(ahd, QNEXTPTR,
  5900. offsetof(struct hardware_scb, next_hscb_busaddr));
  5901. ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
  5902. ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
  5903. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  5904. ahd_outb(ahd, LUNLEN,
  5905. sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
  5906. } else {
  5907. ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
  5908. }
  5909. ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
  5910. ahd_outb(ahd, MAXCMD, 0xFF);
  5911. ahd_outb(ahd, SCBAUTOPTR,
  5912. AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
  5913. /* We haven't been enabled for target mode yet. */
  5914. ahd_outb(ahd, MULTARGID, 0);
  5915. ahd_outb(ahd, MULTARGID + 1, 0);
  5916. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5917. /* Initialize the negotiation table. */
  5918. if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
  5919. /*
  5920. * Clear the spare bytes in the neg table to avoid
  5921. * spurious parity errors.
  5922. */
  5923. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  5924. ahd_outb(ahd, NEGOADDR, target);
  5925. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
  5926. for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
  5927. ahd_outb(ahd, ANNEXDAT, 0);
  5928. }
  5929. }
  5930. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  5931. struct ahd_devinfo devinfo;
  5932. struct ahd_initiator_tinfo *tinfo;
  5933. struct ahd_tmode_tstate *tstate;
  5934. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  5935. target, &tstate);
  5936. ahd_compile_devinfo(&devinfo, ahd->our_id,
  5937. target, CAM_LUN_WILDCARD,
  5938. 'A', ROLE_INITIATOR);
  5939. ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
  5940. }
  5941. ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
  5942. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5943. #ifdef NEEDS_MORE_TESTING
  5944. /*
  5945. * Always enable abort on incoming L_Qs if this feature is
  5946. * supported. We use this to catch invalid SCB references.
  5947. */
  5948. if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
  5949. ahd_outb(ahd, LQCTL1, ABORTPENDING);
  5950. else
  5951. #endif
  5952. ahd_outb(ahd, LQCTL1, 0);
  5953. /* All of our queues are empty */
  5954. ahd->qoutfifonext = 0;
  5955. ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID_LE;
  5956. ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID >> 8);
  5957. for (i = 0; i < AHD_QOUT_SIZE; i++)
  5958. ahd->qoutfifo[i] = 0;
  5959. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
  5960. ahd->qinfifonext = 0;
  5961. for (i = 0; i < AHD_QIN_SIZE; i++)
  5962. ahd->qinfifo[i] = SCB_LIST_NULL;
  5963. if ((ahd->features & AHD_TARGETMODE) != 0) {
  5964. /* All target command blocks start out invalid. */
  5965. for (i = 0; i < AHD_TMODE_CMDS; i++)
  5966. ahd->targetcmds[i].cmd_valid = 0;
  5967. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
  5968. ahd->tqinfifonext = 1;
  5969. ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
  5970. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  5971. }
  5972. /* Initialize Scratch Ram. */
  5973. ahd_outb(ahd, SEQ_FLAGS, 0);
  5974. ahd_outb(ahd, SEQ_FLAGS2, 0);
  5975. /* We don't have any waiting selections */
  5976. ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
  5977. ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
  5978. for (i = 0; i < AHD_NUM_TARGETS; i++)
  5979. ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
  5980. /*
  5981. * Nobody is waiting to be DMAed into the QOUTFIFO.
  5982. */
  5983. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  5984. ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
  5985. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  5986. /*
  5987. * The Freeze Count is 0.
  5988. */
  5989. ahd_outw(ahd, QFREEZE_COUNT, 0);
  5990. /*
  5991. * Tell the sequencer where it can find our arrays in memory.
  5992. */
  5993. busaddr = ahd->shared_data_busaddr;
  5994. ahd_outb(ahd, SHARED_DATA_ADDR, busaddr & 0xFF);
  5995. ahd_outb(ahd, SHARED_DATA_ADDR + 1, (busaddr >> 8) & 0xFF);
  5996. ahd_outb(ahd, SHARED_DATA_ADDR + 2, (busaddr >> 16) & 0xFF);
  5997. ahd_outb(ahd, SHARED_DATA_ADDR + 3, (busaddr >> 24) & 0xFF);
  5998. ahd_outb(ahd, QOUTFIFO_NEXT_ADDR, busaddr & 0xFF);
  5999. ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 1, (busaddr >> 8) & 0xFF);
  6000. ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 2, (busaddr >> 16) & 0xFF);
  6001. ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 3, (busaddr >> 24) & 0xFF);
  6002. /*
  6003. * Setup the allowed SCSI Sequences based on operational mode.
  6004. * If we are a target, we'll enable select in operations once
  6005. * we've had a lun enabled.
  6006. */
  6007. scsiseq_template = ENAUTOATNP;
  6008. if ((ahd->flags & AHD_INITIATORROLE) != 0)
  6009. scsiseq_template |= ENRSELI;
  6010. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
  6011. /* There are no busy SCBs yet. */
  6012. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  6013. int lun;
  6014. for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
  6015. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
  6016. }
  6017. /*
  6018. * Initialize the group code to command length table.
  6019. * Vendor Unique codes are set to 0 so we only capture
  6020. * the first byte of the cdb. These can be overridden
  6021. * when target mode is enabled.
  6022. */
  6023. ahd_outb(ahd, CMDSIZE_TABLE, 5);
  6024. ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
  6025. ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
  6026. ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
  6027. ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
  6028. ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
  6029. ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
  6030. ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
  6031. /* Tell the sequencer of our initial queue positions */
  6032. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6033. ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
  6034. ahd->qinfifonext = 0;
  6035. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6036. ahd_set_hescb_qoff(ahd, 0);
  6037. ahd_set_snscb_qoff(ahd, 0);
  6038. ahd_set_sescb_qoff(ahd, 0);
  6039. ahd_set_sdscb_qoff(ahd, 0);
  6040. /*
  6041. * Tell the sequencer which SCB will be the next one it receives.
  6042. */
  6043. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6044. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
  6045. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
  6046. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
  6047. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
  6048. /*
  6049. * Default to coalescing disabled.
  6050. */
  6051. ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
  6052. ahd_outw(ahd, CMDS_PENDING, 0);
  6053. ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
  6054. ahd->int_coalescing_maxcmds,
  6055. ahd->int_coalescing_mincmds);
  6056. ahd_enable_coalescing(ahd, FALSE);
  6057. ahd_loadseq(ahd);
  6058. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6059. }
  6060. /*
  6061. * Setup default device and controller settings.
  6062. * This should only be called if our probe has
  6063. * determined that no configuration data is available.
  6064. */
  6065. int
  6066. ahd_default_config(struct ahd_softc *ahd)
  6067. {
  6068. int targ;
  6069. ahd->our_id = 7;
  6070. /*
  6071. * Allocate a tstate to house information for our
  6072. * initiator presence on the bus as well as the user
  6073. * data for any target mode initiator.
  6074. */
  6075. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6076. printf("%s: unable to allocate ahd_tmode_tstate. "
  6077. "Failing attach\n", ahd_name(ahd));
  6078. return (ENOMEM);
  6079. }
  6080. for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
  6081. struct ahd_devinfo devinfo;
  6082. struct ahd_initiator_tinfo *tinfo;
  6083. struct ahd_tmode_tstate *tstate;
  6084. uint16_t target_mask;
  6085. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6086. targ, &tstate);
  6087. /*
  6088. * We support SPC2 and SPI4.
  6089. */
  6090. tinfo->user.protocol_version = 4;
  6091. tinfo->user.transport_version = 4;
  6092. target_mask = 0x01 << targ;
  6093. ahd->user_discenable |= target_mask;
  6094. tstate->discenable |= target_mask;
  6095. ahd->user_tagenable |= target_mask;
  6096. #ifdef AHD_FORCE_160
  6097. tinfo->user.period = AHD_SYNCRATE_DT;
  6098. #else
  6099. tinfo->user.period = AHD_SYNCRATE_160;
  6100. #endif
  6101. tinfo->user.offset = MAX_OFFSET;
  6102. tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM
  6103. | MSG_EXT_PPR_WR_FLOW
  6104. | MSG_EXT_PPR_HOLD_MCS
  6105. | MSG_EXT_PPR_IU_REQ
  6106. | MSG_EXT_PPR_QAS_REQ
  6107. | MSG_EXT_PPR_DT_REQ;
  6108. if ((ahd->features & AHD_RTI) != 0)
  6109. tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
  6110. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  6111. /*
  6112. * Start out Async/Narrow/Untagged and with
  6113. * conservative protocol support.
  6114. */
  6115. tinfo->goal.protocol_version = 2;
  6116. tinfo->goal.transport_version = 2;
  6117. tinfo->curr.protocol_version = 2;
  6118. tinfo->curr.transport_version = 2;
  6119. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6120. targ, CAM_LUN_WILDCARD,
  6121. 'A', ROLE_INITIATOR);
  6122. tstate->tagenable &= ~target_mask;
  6123. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6124. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6125. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6126. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6127. /*paused*/TRUE);
  6128. }
  6129. return (0);
  6130. }
  6131. /*
  6132. * Parse device configuration information.
  6133. */
  6134. int
  6135. ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
  6136. {
  6137. int targ;
  6138. int max_targ;
  6139. max_targ = sc->max_targets & CFMAXTARG;
  6140. ahd->our_id = sc->brtime_id & CFSCSIID;
  6141. /*
  6142. * Allocate a tstate to house information for our
  6143. * initiator presence on the bus as well as the user
  6144. * data for any target mode initiator.
  6145. */
  6146. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6147. printf("%s: unable to allocate ahd_tmode_tstate. "
  6148. "Failing attach\n", ahd_name(ahd));
  6149. return (ENOMEM);
  6150. }
  6151. for (targ = 0; targ < max_targ; targ++) {
  6152. struct ahd_devinfo devinfo;
  6153. struct ahd_initiator_tinfo *tinfo;
  6154. struct ahd_transinfo *user_tinfo;
  6155. struct ahd_tmode_tstate *tstate;
  6156. uint16_t target_mask;
  6157. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6158. targ, &tstate);
  6159. user_tinfo = &tinfo->user;
  6160. /*
  6161. * We support SPC2 and SPI4.
  6162. */
  6163. tinfo->user.protocol_version = 4;
  6164. tinfo->user.transport_version = 4;
  6165. target_mask = 0x01 << targ;
  6166. ahd->user_discenable &= ~target_mask;
  6167. tstate->discenable &= ~target_mask;
  6168. ahd->user_tagenable &= ~target_mask;
  6169. if (sc->device_flags[targ] & CFDISC) {
  6170. tstate->discenable |= target_mask;
  6171. ahd->user_discenable |= target_mask;
  6172. ahd->user_tagenable |= target_mask;
  6173. } else {
  6174. /*
  6175. * Cannot be packetized without disconnection.
  6176. */
  6177. sc->device_flags[targ] &= ~CFPACKETIZED;
  6178. }
  6179. user_tinfo->ppr_options = 0;
  6180. user_tinfo->period = (sc->device_flags[targ] & CFXFER);
  6181. if (user_tinfo->period < CFXFER_ASYNC) {
  6182. if (user_tinfo->period <= AHD_PERIOD_10MHz)
  6183. user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
  6184. user_tinfo->offset = MAX_OFFSET;
  6185. } else {
  6186. user_tinfo->offset = 0;
  6187. user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
  6188. }
  6189. #ifdef AHD_FORCE_160
  6190. if (user_tinfo->period <= AHD_SYNCRATE_160)
  6191. user_tinfo->period = AHD_SYNCRATE_DT;
  6192. #endif
  6193. if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
  6194. user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM
  6195. | MSG_EXT_PPR_WR_FLOW
  6196. | MSG_EXT_PPR_HOLD_MCS
  6197. | MSG_EXT_PPR_IU_REQ;
  6198. if ((ahd->features & AHD_RTI) != 0)
  6199. user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
  6200. }
  6201. if ((sc->device_flags[targ] & CFQAS) != 0)
  6202. user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
  6203. if ((sc->device_flags[targ] & CFWIDEB) != 0)
  6204. user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
  6205. else
  6206. user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
  6207. #ifdef AHD_DEBUG
  6208. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  6209. printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
  6210. user_tinfo->period, user_tinfo->offset,
  6211. user_tinfo->ppr_options);
  6212. #endif
  6213. /*
  6214. * Start out Async/Narrow/Untagged and with
  6215. * conservative protocol support.
  6216. */
  6217. tstate->tagenable &= ~target_mask;
  6218. tinfo->goal.protocol_version = 2;
  6219. tinfo->goal.transport_version = 2;
  6220. tinfo->curr.protocol_version = 2;
  6221. tinfo->curr.transport_version = 2;
  6222. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6223. targ, CAM_LUN_WILDCARD,
  6224. 'A', ROLE_INITIATOR);
  6225. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6226. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6227. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6228. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6229. /*paused*/TRUE);
  6230. }
  6231. ahd->flags &= ~AHD_SPCHK_ENB_A;
  6232. if (sc->bios_control & CFSPARITY)
  6233. ahd->flags |= AHD_SPCHK_ENB_A;
  6234. ahd->flags &= ~AHD_RESET_BUS_A;
  6235. if (sc->bios_control & CFRESETB)
  6236. ahd->flags |= AHD_RESET_BUS_A;
  6237. ahd->flags &= ~AHD_EXTENDED_TRANS_A;
  6238. if (sc->bios_control & CFEXTEND)
  6239. ahd->flags |= AHD_EXTENDED_TRANS_A;
  6240. ahd->flags &= ~AHD_BIOS_ENABLED;
  6241. if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
  6242. ahd->flags |= AHD_BIOS_ENABLED;
  6243. ahd->flags &= ~AHD_STPWLEVEL_A;
  6244. if ((sc->adapter_control & CFSTPWLEVEL) != 0)
  6245. ahd->flags |= AHD_STPWLEVEL_A;
  6246. return (0);
  6247. }
  6248. /*
  6249. * Parse device configuration information.
  6250. */
  6251. int
  6252. ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
  6253. {
  6254. int error;
  6255. error = ahd_verify_vpd_cksum(vpd);
  6256. if (error == 0)
  6257. return (EINVAL);
  6258. if ((vpd->bios_flags & VPDBOOTHOST) != 0)
  6259. ahd->flags |= AHD_BOOT_CHANNEL;
  6260. return (0);
  6261. }
  6262. void
  6263. ahd_intr_enable(struct ahd_softc *ahd, int enable)
  6264. {
  6265. u_int hcntrl;
  6266. hcntrl = ahd_inb(ahd, HCNTRL);
  6267. hcntrl &= ~INTEN;
  6268. ahd->pause &= ~INTEN;
  6269. ahd->unpause &= ~INTEN;
  6270. if (enable) {
  6271. hcntrl |= INTEN;
  6272. ahd->pause |= INTEN;
  6273. ahd->unpause |= INTEN;
  6274. }
  6275. ahd_outb(ahd, HCNTRL, hcntrl);
  6276. }
  6277. void
  6278. ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
  6279. u_int mincmds)
  6280. {
  6281. if (timer > AHD_TIMER_MAX_US)
  6282. timer = AHD_TIMER_MAX_US;
  6283. ahd->int_coalescing_timer = timer;
  6284. if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
  6285. maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
  6286. if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
  6287. mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
  6288. ahd->int_coalescing_maxcmds = maxcmds;
  6289. ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
  6290. ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
  6291. ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
  6292. }
  6293. void
  6294. ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
  6295. {
  6296. ahd->hs_mailbox &= ~ENINT_COALESCE;
  6297. if (enable)
  6298. ahd->hs_mailbox |= ENINT_COALESCE;
  6299. ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
  6300. ahd_flush_device_writes(ahd);
  6301. ahd_run_qoutfifo(ahd);
  6302. }
  6303. /*
  6304. * Ensure that the card is paused in a location
  6305. * outside of all critical sections and that all
  6306. * pending work is completed prior to returning.
  6307. * This routine should only be called from outside
  6308. * an interrupt context.
  6309. */
  6310. void
  6311. ahd_pause_and_flushwork(struct ahd_softc *ahd)
  6312. {
  6313. u_int intstat;
  6314. u_int maxloops;
  6315. u_int qfreeze_cnt;
  6316. maxloops = 1000;
  6317. ahd->flags |= AHD_ALL_INTERRUPTS;
  6318. ahd_pause(ahd);
  6319. /*
  6320. * Increment the QFreeze Count so that the sequencer
  6321. * will not start new selections. We do this only
  6322. * until we are safely paused without further selections
  6323. * pending.
  6324. */
  6325. ahd_outw(ahd, QFREEZE_COUNT, ahd_inw(ahd, QFREEZE_COUNT) + 1);
  6326. ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
  6327. do {
  6328. struct scb *waiting_scb;
  6329. ahd_unpause(ahd);
  6330. ahd_intr(ahd);
  6331. ahd_pause(ahd);
  6332. ahd_clear_critical_section(ahd);
  6333. intstat = ahd_inb(ahd, INTSTAT);
  6334. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6335. if ((ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
  6336. ahd_outb(ahd, SCSISEQ0,
  6337. ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  6338. /*
  6339. * In the non-packetized case, the sequencer (for Rev A),
  6340. * relies on ENSELO remaining set after SELDO. The hardware
  6341. * auto-clears ENSELO in the packetized case.
  6342. */
  6343. waiting_scb = ahd_lookup_scb(ahd,
  6344. ahd_inw(ahd, WAITING_TID_HEAD));
  6345. if (waiting_scb != NULL
  6346. && (waiting_scb->flags & SCB_PACKETIZED) == 0
  6347. && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)
  6348. ahd_outb(ahd, SCSISEQ0,
  6349. ahd_inb(ahd, SCSISEQ0) | ENSELO);
  6350. } while (--maxloops
  6351. && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
  6352. && ((intstat & INT_PEND) != 0
  6353. || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  6354. || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
  6355. if (maxloops == 0) {
  6356. printf("Infinite interrupt loop, INTSTAT = %x",
  6357. ahd_inb(ahd, INTSTAT));
  6358. }
  6359. qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
  6360. if (qfreeze_cnt == 0) {
  6361. printf("%s: ahd_pause_and_flushwork with 0 qfreeze count!\n",
  6362. ahd_name(ahd));
  6363. } else {
  6364. qfreeze_cnt--;
  6365. }
  6366. ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
  6367. if (qfreeze_cnt == 0)
  6368. ahd_outb(ahd, SEQ_FLAGS2,
  6369. ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
  6370. ahd_flush_qoutfifo(ahd);
  6371. ahd_platform_flushwork(ahd);
  6372. ahd->flags &= ~AHD_ALL_INTERRUPTS;
  6373. }
  6374. int
  6375. ahd_suspend(struct ahd_softc *ahd)
  6376. {
  6377. ahd_pause_and_flushwork(ahd);
  6378. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  6379. ahd_unpause(ahd);
  6380. return (EBUSY);
  6381. }
  6382. ahd_shutdown(ahd);
  6383. return (0);
  6384. }
  6385. int
  6386. ahd_resume(struct ahd_softc *ahd)
  6387. {
  6388. ahd_reset(ahd, /*reinit*/TRUE);
  6389. ahd_intr_enable(ahd, TRUE);
  6390. ahd_restart(ahd);
  6391. return (0);
  6392. }
  6393. /************************** Busy Target Table *********************************/
  6394. /*
  6395. * Set SCBPTR to the SCB that contains the busy
  6396. * table entry for TCL. Return the offset into
  6397. * the SCB that contains the entry for TCL.
  6398. * saved_scbid is dereferenced and set to the
  6399. * scbid that should be restored once manipualtion
  6400. * of the TCL entry is complete.
  6401. */
  6402. static __inline u_int
  6403. ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
  6404. {
  6405. /*
  6406. * Index to the SCB that contains the busy entry.
  6407. */
  6408. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6409. *saved_scbid = ahd_get_scbptr(ahd);
  6410. ahd_set_scbptr(ahd, TCL_LUN(tcl)
  6411. | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
  6412. /*
  6413. * And now calculate the SCB offset to the entry.
  6414. * Each entry is 2 bytes wide, hence the
  6415. * multiplication by 2.
  6416. */
  6417. return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
  6418. }
  6419. /*
  6420. * Return the untagged transaction id for a given target/channel lun.
  6421. */
  6422. u_int
  6423. ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
  6424. {
  6425. u_int scbid;
  6426. u_int scb_offset;
  6427. u_int saved_scbptr;
  6428. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6429. scbid = ahd_inw_scbram(ahd, scb_offset);
  6430. ahd_set_scbptr(ahd, saved_scbptr);
  6431. return (scbid);
  6432. }
  6433. void
  6434. ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
  6435. {
  6436. u_int scb_offset;
  6437. u_int saved_scbptr;
  6438. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6439. ahd_outw(ahd, scb_offset, scbid);
  6440. ahd_set_scbptr(ahd, saved_scbptr);
  6441. }
  6442. /************************** SCB and SCB queue management **********************/
  6443. int
  6444. ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
  6445. char channel, int lun, u_int tag, role_t role)
  6446. {
  6447. int targ = SCB_GET_TARGET(ahd, scb);
  6448. char chan = SCB_GET_CHANNEL(ahd, scb);
  6449. int slun = SCB_GET_LUN(scb);
  6450. int match;
  6451. match = ((chan == channel) || (channel == ALL_CHANNELS));
  6452. if (match != 0)
  6453. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  6454. if (match != 0)
  6455. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  6456. if (match != 0) {
  6457. #ifdef AHD_TARGET_MODE
  6458. int group;
  6459. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  6460. if (role == ROLE_INITIATOR) {
  6461. match = (group != XPT_FC_GROUP_TMODE)
  6462. && ((tag == SCB_GET_TAG(scb))
  6463. || (tag == SCB_LIST_NULL));
  6464. } else if (role == ROLE_TARGET) {
  6465. match = (group == XPT_FC_GROUP_TMODE)
  6466. && ((tag == scb->io_ctx->csio.tag_id)
  6467. || (tag == SCB_LIST_NULL));
  6468. }
  6469. #else /* !AHD_TARGET_MODE */
  6470. match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
  6471. #endif /* AHD_TARGET_MODE */
  6472. }
  6473. return match;
  6474. }
  6475. void
  6476. ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
  6477. {
  6478. int target;
  6479. char channel;
  6480. int lun;
  6481. target = SCB_GET_TARGET(ahd, scb);
  6482. lun = SCB_GET_LUN(scb);
  6483. channel = SCB_GET_CHANNEL(ahd, scb);
  6484. ahd_search_qinfifo(ahd, target, channel, lun,
  6485. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  6486. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  6487. ahd_platform_freeze_devq(ahd, scb);
  6488. }
  6489. void
  6490. ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
  6491. {
  6492. struct scb *prev_scb;
  6493. ahd_mode_state saved_modes;
  6494. saved_modes = ahd_save_modes(ahd);
  6495. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6496. prev_scb = NULL;
  6497. if (ahd_qinfifo_count(ahd) != 0) {
  6498. u_int prev_tag;
  6499. u_int prev_pos;
  6500. prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
  6501. prev_tag = ahd->qinfifo[prev_pos];
  6502. prev_scb = ahd_lookup_scb(ahd, prev_tag);
  6503. }
  6504. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6505. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6506. ahd_restore_modes(ahd, saved_modes);
  6507. }
  6508. static void
  6509. ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
  6510. struct scb *scb)
  6511. {
  6512. if (prev_scb == NULL) {
  6513. uint32_t busaddr;
  6514. busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
  6515. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
  6516. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
  6517. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
  6518. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
  6519. } else {
  6520. prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
  6521. ahd_sync_scb(ahd, prev_scb,
  6522. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6523. }
  6524. ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
  6525. ahd->qinfifonext++;
  6526. scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
  6527. ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6528. }
  6529. static int
  6530. ahd_qinfifo_count(struct ahd_softc *ahd)
  6531. {
  6532. u_int qinpos;
  6533. u_int wrap_qinpos;
  6534. u_int wrap_qinfifonext;
  6535. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6536. qinpos = ahd_get_snscb_qoff(ahd);
  6537. wrap_qinpos = AHD_QIN_WRAP(qinpos);
  6538. wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
  6539. if (wrap_qinfifonext >= wrap_qinpos)
  6540. return (wrap_qinfifonext - wrap_qinpos);
  6541. else
  6542. return (wrap_qinfifonext
  6543. + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
  6544. }
  6545. void
  6546. ahd_reset_cmds_pending(struct ahd_softc *ahd)
  6547. {
  6548. struct scb *scb;
  6549. ahd_mode_state saved_modes;
  6550. u_int pending_cmds;
  6551. saved_modes = ahd_save_modes(ahd);
  6552. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6553. /*
  6554. * Don't count any commands as outstanding that the
  6555. * sequencer has already marked for completion.
  6556. */
  6557. ahd_flush_qoutfifo(ahd);
  6558. pending_cmds = 0;
  6559. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  6560. pending_cmds++;
  6561. }
  6562. ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
  6563. ahd_restore_modes(ahd, saved_modes);
  6564. ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
  6565. }
  6566. int
  6567. ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
  6568. int lun, u_int tag, role_t role, uint32_t status,
  6569. ahd_search_action action)
  6570. {
  6571. struct scb *scb;
  6572. struct scb *prev_scb;
  6573. ahd_mode_state saved_modes;
  6574. u_int qinstart;
  6575. u_int qinpos;
  6576. u_int qintail;
  6577. u_int tid_next;
  6578. u_int tid_prev;
  6579. u_int scbid;
  6580. u_int savedscbptr;
  6581. uint32_t busaddr;
  6582. int found;
  6583. int targets;
  6584. /* Must be in CCHAN mode */
  6585. saved_modes = ahd_save_modes(ahd);
  6586. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6587. /*
  6588. * Halt any pending SCB DMA. The sequencer will reinitiate
  6589. * this dma if the qinfifo is not empty once we unpause.
  6590. */
  6591. if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
  6592. == (CCARREN|CCSCBEN|CCSCBDIR)) {
  6593. ahd_outb(ahd, CCSCBCTL,
  6594. ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
  6595. while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
  6596. ;
  6597. }
  6598. /* Determine sequencer's position in the qinfifo. */
  6599. qintail = AHD_QIN_WRAP(ahd->qinfifonext);
  6600. qinstart = ahd_get_snscb_qoff(ahd);
  6601. qinpos = AHD_QIN_WRAP(qinstart);
  6602. found = 0;
  6603. prev_scb = NULL;
  6604. if (action == SEARCH_PRINT) {
  6605. printf("qinstart = %d qinfifonext = %d\nQINFIFO:",
  6606. qinstart, ahd->qinfifonext);
  6607. }
  6608. /*
  6609. * Start with an empty queue. Entries that are not chosen
  6610. * for removal will be re-added to the queue as we go.
  6611. */
  6612. ahd->qinfifonext = qinstart;
  6613. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6614. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
  6615. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
  6616. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
  6617. ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
  6618. while (qinpos != qintail) {
  6619. scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
  6620. if (scb == NULL) {
  6621. printf("qinpos = %d, SCB index = %d\n",
  6622. qinpos, ahd->qinfifo[qinpos]);
  6623. panic("Loop 1\n");
  6624. }
  6625. if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
  6626. /*
  6627. * We found an scb that needs to be acted on.
  6628. */
  6629. found++;
  6630. switch (action) {
  6631. case SEARCH_COMPLETE:
  6632. {
  6633. cam_status ostat;
  6634. cam_status cstat;
  6635. ostat = ahd_get_transaction_status(scb);
  6636. if (ostat == CAM_REQ_INPROG)
  6637. ahd_set_transaction_status(scb,
  6638. status);
  6639. cstat = ahd_get_transaction_status(scb);
  6640. if (cstat != CAM_REQ_CMP)
  6641. ahd_freeze_scb(scb);
  6642. if ((scb->flags & SCB_ACTIVE) == 0)
  6643. printf("Inactive SCB in qinfifo\n");
  6644. ahd_done(ahd, scb);
  6645. /* FALLTHROUGH */
  6646. }
  6647. case SEARCH_REMOVE:
  6648. break;
  6649. case SEARCH_PRINT:
  6650. printf(" 0x%x", ahd->qinfifo[qinpos]);
  6651. /* FALLTHROUGH */
  6652. case SEARCH_COUNT:
  6653. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6654. prev_scb = scb;
  6655. break;
  6656. }
  6657. } else {
  6658. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6659. prev_scb = scb;
  6660. }
  6661. qinpos = AHD_QIN_WRAP(qinpos+1);
  6662. }
  6663. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6664. if (action == SEARCH_PRINT)
  6665. printf("\nWAITING_TID_QUEUES:\n");
  6666. /*
  6667. * Search waiting for selection lists. We traverse the
  6668. * list of "their ids" waiting for selection and, if
  6669. * appropriate, traverse the SCBs of each "their id"
  6670. * looking for matches.
  6671. */
  6672. savedscbptr = ahd_get_scbptr(ahd);
  6673. tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
  6674. tid_prev = SCB_LIST_NULL;
  6675. targets = 0;
  6676. for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
  6677. u_int tid_head;
  6678. /*
  6679. * We limit based on the number of SCBs since
  6680. * MK_MESSAGE SCBs are not in the per-tid lists.
  6681. */
  6682. targets++;
  6683. if (targets > AHD_SCB_MAX) {
  6684. panic("TID LIST LOOP");
  6685. }
  6686. if (scbid >= ahd->scb_data.numscbs) {
  6687. printf("%s: Waiting TID List inconsistency. "
  6688. "SCB index == 0x%x, yet numscbs == 0x%x.",
  6689. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6690. ahd_dump_card_state(ahd);
  6691. panic("for safety");
  6692. }
  6693. scb = ahd_lookup_scb(ahd, scbid);
  6694. if (scb == NULL) {
  6695. printf("%s: SCB = 0x%x Not Active!\n",
  6696. ahd_name(ahd), scbid);
  6697. panic("Waiting TID List traversal\n");
  6698. }
  6699. ahd_set_scbptr(ahd, scbid);
  6700. tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
  6701. if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
  6702. SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
  6703. tid_prev = scbid;
  6704. continue;
  6705. }
  6706. /*
  6707. * We found a list of scbs that needs to be searched.
  6708. */
  6709. if (action == SEARCH_PRINT)
  6710. printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
  6711. tid_head = scbid;
  6712. found += ahd_search_scb_list(ahd, target, channel,
  6713. lun, tag, role, status,
  6714. action, &tid_head,
  6715. SCB_GET_TARGET(ahd, scb));
  6716. if (tid_head != scbid)
  6717. ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
  6718. if (!SCBID_IS_NULL(tid_head))
  6719. tid_prev = tid_head;
  6720. if (action == SEARCH_PRINT)
  6721. printf(")\n");
  6722. }
  6723. ahd_set_scbptr(ahd, savedscbptr);
  6724. ahd_restore_modes(ahd, saved_modes);
  6725. return (found);
  6726. }
  6727. static int
  6728. ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
  6729. int lun, u_int tag, role_t role, uint32_t status,
  6730. ahd_search_action action, u_int *list_head, u_int tid)
  6731. {
  6732. struct scb *scb;
  6733. u_int scbid;
  6734. u_int next;
  6735. u_int prev;
  6736. int found;
  6737. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6738. found = 0;
  6739. prev = SCB_LIST_NULL;
  6740. next = *list_head;
  6741. for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
  6742. if (scbid >= ahd->scb_data.numscbs) {
  6743. printf("%s:SCB List inconsistency. "
  6744. "SCB == 0x%x, yet numscbs == 0x%x.",
  6745. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6746. ahd_dump_card_state(ahd);
  6747. panic("for safety");
  6748. }
  6749. scb = ahd_lookup_scb(ahd, scbid);
  6750. if (scb == NULL) {
  6751. printf("%s: SCB = %d Not Active!\n",
  6752. ahd_name(ahd), scbid);
  6753. panic("Waiting List traversal\n");
  6754. }
  6755. ahd_set_scbptr(ahd, scbid);
  6756. next = ahd_inw_scbram(ahd, SCB_NEXT);
  6757. if (ahd_match_scb(ahd, scb, target, channel,
  6758. lun, SCB_LIST_NULL, role) == 0) {
  6759. prev = scbid;
  6760. continue;
  6761. }
  6762. found++;
  6763. switch (action) {
  6764. case SEARCH_COMPLETE:
  6765. {
  6766. cam_status ostat;
  6767. cam_status cstat;
  6768. ostat = ahd_get_transaction_status(scb);
  6769. if (ostat == CAM_REQ_INPROG)
  6770. ahd_set_transaction_status(scb, status);
  6771. cstat = ahd_get_transaction_status(scb);
  6772. if (cstat != CAM_REQ_CMP)
  6773. ahd_freeze_scb(scb);
  6774. if ((scb->flags & SCB_ACTIVE) == 0)
  6775. printf("Inactive SCB in Waiting List\n");
  6776. ahd_done(ahd, scb);
  6777. /* FALLTHROUGH */
  6778. }
  6779. case SEARCH_REMOVE:
  6780. ahd_rem_wscb(ahd, scbid, prev, next, tid);
  6781. if (prev == SCB_LIST_NULL)
  6782. *list_head = next;
  6783. break;
  6784. case SEARCH_PRINT:
  6785. printf("0x%x ", scbid);
  6786. case SEARCH_COUNT:
  6787. prev = scbid;
  6788. break;
  6789. }
  6790. if (found > AHD_SCB_MAX)
  6791. panic("SCB LIST LOOP");
  6792. }
  6793. if (action == SEARCH_COMPLETE
  6794. || action == SEARCH_REMOVE)
  6795. ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
  6796. return (found);
  6797. }
  6798. static void
  6799. ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
  6800. u_int tid_cur, u_int tid_next)
  6801. {
  6802. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6803. if (SCBID_IS_NULL(tid_cur)) {
  6804. /* Bypass current TID list */
  6805. if (SCBID_IS_NULL(tid_prev)) {
  6806. ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
  6807. } else {
  6808. ahd_set_scbptr(ahd, tid_prev);
  6809. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6810. }
  6811. if (SCBID_IS_NULL(tid_next))
  6812. ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
  6813. } else {
  6814. /* Stitch through tid_cur */
  6815. if (SCBID_IS_NULL(tid_prev)) {
  6816. ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
  6817. } else {
  6818. ahd_set_scbptr(ahd, tid_prev);
  6819. ahd_outw(ahd, SCB_NEXT2, tid_cur);
  6820. }
  6821. ahd_set_scbptr(ahd, tid_cur);
  6822. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6823. if (SCBID_IS_NULL(tid_next))
  6824. ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
  6825. }
  6826. }
  6827. /*
  6828. * Manipulate the waiting for selection list and return the
  6829. * scb that follows the one that we remove.
  6830. */
  6831. static u_int
  6832. ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  6833. u_int prev, u_int next, u_int tid)
  6834. {
  6835. u_int tail_offset;
  6836. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6837. if (!SCBID_IS_NULL(prev)) {
  6838. ahd_set_scbptr(ahd, prev);
  6839. ahd_outw(ahd, SCB_NEXT, next);
  6840. }
  6841. /*
  6842. * SCBs that had MK_MESSAGE set in them will not
  6843. * be queued to the per-target lists, so don't
  6844. * blindly clear the tail pointer.
  6845. */
  6846. tail_offset = WAITING_SCB_TAILS + (2 * tid);
  6847. if (SCBID_IS_NULL(next)
  6848. && ahd_inw(ahd, tail_offset) == scbid)
  6849. ahd_outw(ahd, tail_offset, prev);
  6850. ahd_add_scb_to_free_list(ahd, scbid);
  6851. return (next);
  6852. }
  6853. /*
  6854. * Add the SCB as selected by SCBPTR onto the on chip list of
  6855. * free hardware SCBs. This list is empty/unused if we are not
  6856. * performing SCB paging.
  6857. */
  6858. static void
  6859. ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
  6860. {
  6861. /* XXX Need some other mechanism to designate "free". */
  6862. /*
  6863. * Invalidate the tag so that our abort
  6864. * routines don't think it's active.
  6865. ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
  6866. */
  6867. }
  6868. /******************************** Error Handling ******************************/
  6869. /*
  6870. * Abort all SCBs that match the given description (target/channel/lun/tag),
  6871. * setting their status to the passed in status if the status has not already
  6872. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  6873. * is paused before it is called.
  6874. */
  6875. int
  6876. ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
  6877. int lun, u_int tag, role_t role, uint32_t status)
  6878. {
  6879. struct scb *scbp;
  6880. struct scb *scbp_next;
  6881. u_int i, j;
  6882. u_int maxtarget;
  6883. u_int minlun;
  6884. u_int maxlun;
  6885. int found;
  6886. ahd_mode_state saved_modes;
  6887. /* restore this when we're done */
  6888. saved_modes = ahd_save_modes(ahd);
  6889. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6890. found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
  6891. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  6892. /*
  6893. * Clean out the busy target table for any untagged commands.
  6894. */
  6895. i = 0;
  6896. maxtarget = 16;
  6897. if (target != CAM_TARGET_WILDCARD) {
  6898. i = target;
  6899. if (channel == 'B')
  6900. i += 8;
  6901. maxtarget = i + 1;
  6902. }
  6903. if (lun == CAM_LUN_WILDCARD) {
  6904. minlun = 0;
  6905. maxlun = AHD_NUM_LUNS_NONPKT;
  6906. } else if (lun >= AHD_NUM_LUNS_NONPKT) {
  6907. minlun = maxlun = 0;
  6908. } else {
  6909. minlun = lun;
  6910. maxlun = lun + 1;
  6911. }
  6912. if (role != ROLE_TARGET) {
  6913. for (;i < maxtarget; i++) {
  6914. for (j = minlun;j < maxlun; j++) {
  6915. u_int scbid;
  6916. u_int tcl;
  6917. tcl = BUILD_TCL_RAW(i, 'A', j);
  6918. scbid = ahd_find_busy_tcl(ahd, tcl);
  6919. scbp = ahd_lookup_scb(ahd, scbid);
  6920. if (scbp == NULL
  6921. || ahd_match_scb(ahd, scbp, target, channel,
  6922. lun, tag, role) == 0)
  6923. continue;
  6924. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
  6925. }
  6926. }
  6927. }
  6928. /*
  6929. * Don't abort commands that have already completed,
  6930. * but haven't quite made it up to the host yet.
  6931. */
  6932. ahd_flush_qoutfifo(ahd);
  6933. /*
  6934. * Go through the pending CCB list and look for
  6935. * commands for this target that are still active.
  6936. * These are other tagged commands that were
  6937. * disconnected when the reset occurred.
  6938. */
  6939. scbp_next = LIST_FIRST(&ahd->pending_scbs);
  6940. while (scbp_next != NULL) {
  6941. scbp = scbp_next;
  6942. scbp_next = LIST_NEXT(scbp, pending_links);
  6943. if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
  6944. cam_status ostat;
  6945. ostat = ahd_get_transaction_status(scbp);
  6946. if (ostat == CAM_REQ_INPROG)
  6947. ahd_set_transaction_status(scbp, status);
  6948. if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
  6949. ahd_freeze_scb(scbp);
  6950. if ((scbp->flags & SCB_ACTIVE) == 0)
  6951. printf("Inactive SCB on pending list\n");
  6952. ahd_done(ahd, scbp);
  6953. found++;
  6954. }
  6955. }
  6956. ahd_restore_modes(ahd, saved_modes);
  6957. ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
  6958. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  6959. return found;
  6960. }
  6961. static void
  6962. ahd_reset_current_bus(struct ahd_softc *ahd)
  6963. {
  6964. uint8_t scsiseq;
  6965. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6966. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
  6967. scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
  6968. ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
  6969. ahd_flush_device_writes(ahd);
  6970. ahd_delay(AHD_BUSRESET_DELAY);
  6971. /* Turn off the bus reset */
  6972. ahd_outb(ahd, SCSISEQ0, scsiseq);
  6973. ahd_flush_device_writes(ahd);
  6974. ahd_delay(AHD_BUSRESET_DELAY);
  6975. if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
  6976. /*
  6977. * 2A Razor #474
  6978. * Certain chip state is not cleared for
  6979. * SCSI bus resets that we initiate, so
  6980. * we must reset the chip.
  6981. */
  6982. ahd_reset(ahd, /*reinit*/TRUE);
  6983. ahd_intr_enable(ahd, /*enable*/TRUE);
  6984. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6985. }
  6986. ahd_clear_intstat(ahd);
  6987. }
  6988. int
  6989. ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
  6990. {
  6991. struct ahd_devinfo devinfo;
  6992. u_int initiator;
  6993. u_int target;
  6994. u_int max_scsiid;
  6995. int found;
  6996. u_int fifo;
  6997. u_int next_fifo;
  6998. ahd->pending_device = NULL;
  6999. ahd_compile_devinfo(&devinfo,
  7000. CAM_TARGET_WILDCARD,
  7001. CAM_TARGET_WILDCARD,
  7002. CAM_LUN_WILDCARD,
  7003. channel, ROLE_UNKNOWN);
  7004. ahd_pause(ahd);
  7005. /* Make sure the sequencer is in a safe location. */
  7006. ahd_clear_critical_section(ahd);
  7007. #ifdef AHD_TARGET_MODE
  7008. if ((ahd->flags & AHD_TARGETROLE) != 0) {
  7009. ahd_run_tqinfifo(ahd, /*paused*/TRUE);
  7010. }
  7011. #endif
  7012. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7013. /*
  7014. * Disable selections so no automatic hardware
  7015. * functions will modify chip state.
  7016. */
  7017. ahd_outb(ahd, SCSISEQ0, 0);
  7018. ahd_outb(ahd, SCSISEQ1, 0);
  7019. /*
  7020. * Safely shut down our DMA engines. Always start with
  7021. * the FIFO that is not currently active (if any are
  7022. * actively connected).
  7023. */
  7024. next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  7025. if (next_fifo > CURRFIFO_1)
  7026. /* If disconneced, arbitrarily start with FIFO1. */
  7027. next_fifo = fifo = 0;
  7028. do {
  7029. next_fifo ^= CURRFIFO_1;
  7030. ahd_set_modes(ahd, next_fifo, next_fifo);
  7031. ahd_outb(ahd, DFCNTRL,
  7032. ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
  7033. while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
  7034. ahd_delay(10);
  7035. /*
  7036. * Set CURRFIFO to the now inactive channel.
  7037. */
  7038. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7039. ahd_outb(ahd, DFFSTAT, next_fifo);
  7040. } while (next_fifo != fifo);
  7041. /*
  7042. * Reset the bus if we are initiating this reset
  7043. */
  7044. ahd_clear_msg_state(ahd);
  7045. ahd_outb(ahd, SIMODE1,
  7046. ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST|ENBUSFREE));
  7047. if (initiate_reset)
  7048. ahd_reset_current_bus(ahd);
  7049. ahd_clear_intstat(ahd);
  7050. /*
  7051. * Clean up all the state information for the
  7052. * pending transactions on this bus.
  7053. */
  7054. found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
  7055. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  7056. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  7057. /*
  7058. * Cleanup anything left in the FIFOs.
  7059. */
  7060. ahd_clear_fifo(ahd, 0);
  7061. ahd_clear_fifo(ahd, 1);
  7062. /*
  7063. * Revert to async/narrow transfers until we renegotiate.
  7064. */
  7065. max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
  7066. for (target = 0; target <= max_scsiid; target++) {
  7067. if (ahd->enabled_targets[target] == NULL)
  7068. continue;
  7069. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  7070. struct ahd_devinfo devinfo;
  7071. ahd_compile_devinfo(&devinfo, target, initiator,
  7072. CAM_LUN_WILDCARD,
  7073. 'A', ROLE_UNKNOWN);
  7074. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  7075. AHD_TRANS_CUR, /*paused*/TRUE);
  7076. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  7077. /*offset*/0, /*ppr_options*/0,
  7078. AHD_TRANS_CUR, /*paused*/TRUE);
  7079. }
  7080. }
  7081. #ifdef AHD_TARGET_MODE
  7082. max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
  7083. /*
  7084. * Send an immediate notify ccb to all target more peripheral
  7085. * drivers affected by this action.
  7086. */
  7087. for (target = 0; target <= max_scsiid; target++) {
  7088. struct ahd_tmode_tstate* tstate;
  7089. u_int lun;
  7090. tstate = ahd->enabled_targets[target];
  7091. if (tstate == NULL)
  7092. continue;
  7093. for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
  7094. struct ahd_tmode_lstate* lstate;
  7095. lstate = tstate->enabled_luns[lun];
  7096. if (lstate == NULL)
  7097. continue;
  7098. ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
  7099. EVENT_TYPE_BUS_RESET, /*arg*/0);
  7100. ahd_send_lstate_events(ahd, lstate);
  7101. }
  7102. }
  7103. #endif
  7104. /* Notify the XPT that a bus reset occurred */
  7105. ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
  7106. CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
  7107. ahd_restart(ahd);
  7108. /*
  7109. * Freeze the SIMQ until our poller can determine that
  7110. * the bus reset has really gone away. We set the initial
  7111. * timer to 0 to have the check performed as soon as possible
  7112. * from the timer context.
  7113. */
  7114. if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
  7115. ahd->flags |= AHD_RESET_POLL_ACTIVE;
  7116. ahd_freeze_simq(ahd);
  7117. ahd_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
  7118. }
  7119. return (found);
  7120. }
  7121. #define AHD_RESET_POLL_US 1000
  7122. static void
  7123. ahd_reset_poll(void *arg)
  7124. {
  7125. struct ahd_softc *ahd = arg;
  7126. u_int scsiseq1;
  7127. u_long s;
  7128. ahd_lock(ahd, &s);
  7129. ahd_pause(ahd);
  7130. ahd_update_modes(ahd);
  7131. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7132. ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
  7133. if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
  7134. ahd_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
  7135. ahd_reset_poll, ahd);
  7136. ahd_unpause(ahd);
  7137. ahd_unlock(ahd, &s);
  7138. return;
  7139. }
  7140. /* Reset is now low. Complete chip reinitialization. */
  7141. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
  7142. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  7143. ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
  7144. ahd_unpause(ahd);
  7145. ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
  7146. ahd_unlock(ahd, &s);
  7147. ahd_release_simq(ahd);
  7148. }
  7149. /**************************** Statistics Processing ***************************/
  7150. static void
  7151. ahd_stat_timer(void *arg)
  7152. {
  7153. struct ahd_softc *ahd = arg;
  7154. u_long s;
  7155. int enint_coal;
  7156. ahd_lock(ahd, &s);
  7157. enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
  7158. if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
  7159. enint_coal |= ENINT_COALESCE;
  7160. else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
  7161. enint_coal &= ~ENINT_COALESCE;
  7162. if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
  7163. ahd_enable_coalescing(ahd, enint_coal);
  7164. #ifdef AHD_DEBUG
  7165. if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
  7166. printf("%s: Interrupt coalescing "
  7167. "now %sabled. Cmds %d\n",
  7168. ahd_name(ahd),
  7169. (enint_coal & ENINT_COALESCE) ? "en" : "dis",
  7170. ahd->cmdcmplt_total);
  7171. #endif
  7172. }
  7173. ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
  7174. ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
  7175. ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
  7176. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  7177. ahd_stat_timer, ahd);
  7178. ahd_unlock(ahd, &s);
  7179. }
  7180. /****************************** Status Processing *****************************/
  7181. void
  7182. ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
  7183. {
  7184. if (scb->hscb->shared_data.istatus.scsi_status != 0) {
  7185. ahd_handle_scsi_status(ahd, scb);
  7186. } else {
  7187. ahd_calc_residual(ahd, scb);
  7188. ahd_done(ahd, scb);
  7189. }
  7190. }
  7191. void
  7192. ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
  7193. {
  7194. struct hardware_scb *hscb;
  7195. u_int qfreeze_cnt;
  7196. /*
  7197. * The sequencer freezes its select-out queue
  7198. * anytime a SCSI status error occurs. We must
  7199. * handle the error and decrement the QFREEZE count
  7200. * to allow the sequencer to continue.
  7201. */
  7202. hscb = scb->hscb;
  7203. /* Freeze the queue until the client sees the error. */
  7204. ahd_freeze_devq(ahd, scb);
  7205. ahd_freeze_scb(scb);
  7206. qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
  7207. if (qfreeze_cnt == 0) {
  7208. printf("%s: Bad status with 0 qfreeze count!\n", ahd_name(ahd));
  7209. } else {
  7210. qfreeze_cnt--;
  7211. ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
  7212. }
  7213. if (qfreeze_cnt == 0)
  7214. ahd_outb(ahd, SEQ_FLAGS2,
  7215. ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
  7216. /* Don't want to clobber the original sense code */
  7217. if ((scb->flags & SCB_SENSE) != 0) {
  7218. /*
  7219. * Clear the SCB_SENSE Flag and perform
  7220. * a normal command completion.
  7221. */
  7222. scb->flags &= ~SCB_SENSE;
  7223. ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  7224. ahd_done(ahd, scb);
  7225. return;
  7226. }
  7227. ahd_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  7228. ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
  7229. switch (hscb->shared_data.istatus.scsi_status) {
  7230. case STATUS_PKT_SENSE:
  7231. {
  7232. struct scsi_status_iu_header *siu;
  7233. ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
  7234. siu = (struct scsi_status_iu_header *)scb->sense_data;
  7235. ahd_set_scsi_status(scb, siu->status);
  7236. #ifdef AHD_DEBUG
  7237. if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
  7238. ahd_print_path(ahd, scb);
  7239. printf("SCB 0x%x Received PKT Status of 0x%x\n",
  7240. SCB_GET_TAG(scb), siu->status);
  7241. printf("\tflags = 0x%x, sense len = 0x%x, "
  7242. "pktfail = 0x%x\n",
  7243. siu->flags, scsi_4btoul(siu->sense_length),
  7244. scsi_4btoul(siu->pkt_failures_length));
  7245. }
  7246. #endif
  7247. if ((siu->flags & SIU_RSPVALID) != 0) {
  7248. ahd_print_path(ahd, scb);
  7249. if (scsi_4btoul(siu->pkt_failures_length) < 4) {
  7250. printf("Unable to parse pkt_failures\n");
  7251. } else {
  7252. switch (SIU_PKTFAIL_CODE(siu)) {
  7253. case SIU_PFC_NONE:
  7254. printf("No packet failure found\n");
  7255. break;
  7256. case SIU_PFC_CIU_FIELDS_INVALID:
  7257. printf("Invalid Command IU Field\n");
  7258. break;
  7259. case SIU_PFC_TMF_NOT_SUPPORTED:
  7260. printf("TMF not supportd\n");
  7261. break;
  7262. case SIU_PFC_TMF_FAILED:
  7263. printf("TMF failed\n");
  7264. break;
  7265. case SIU_PFC_INVALID_TYPE_CODE:
  7266. printf("Invalid L_Q Type code\n");
  7267. break;
  7268. case SIU_PFC_ILLEGAL_REQUEST:
  7269. printf("Illegal request\n");
  7270. default:
  7271. break;
  7272. }
  7273. }
  7274. if (siu->status == SCSI_STATUS_OK)
  7275. ahd_set_transaction_status(scb,
  7276. CAM_REQ_CMP_ERR);
  7277. }
  7278. if ((siu->flags & SIU_SNSVALID) != 0) {
  7279. scb->flags |= SCB_PKT_SENSE;
  7280. #ifdef AHD_DEBUG
  7281. if ((ahd_debug & AHD_SHOW_SENSE) != 0)
  7282. printf("Sense data available\n");
  7283. #endif
  7284. }
  7285. ahd_done(ahd, scb);
  7286. break;
  7287. }
  7288. case SCSI_STATUS_CMD_TERMINATED:
  7289. case SCSI_STATUS_CHECK_COND:
  7290. {
  7291. struct ahd_devinfo devinfo;
  7292. struct ahd_dma_seg *sg;
  7293. struct scsi_sense *sc;
  7294. struct ahd_initiator_tinfo *targ_info;
  7295. struct ahd_tmode_tstate *tstate;
  7296. struct ahd_transinfo *tinfo;
  7297. #ifdef AHD_DEBUG
  7298. if (ahd_debug & AHD_SHOW_SENSE) {
  7299. ahd_print_path(ahd, scb);
  7300. printf("SCB %d: requests Check Status\n",
  7301. SCB_GET_TAG(scb));
  7302. }
  7303. #endif
  7304. if (ahd_perform_autosense(scb) == 0)
  7305. break;
  7306. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  7307. SCB_GET_TARGET(ahd, scb),
  7308. SCB_GET_LUN(scb),
  7309. SCB_GET_CHANNEL(ahd, scb),
  7310. ROLE_INITIATOR);
  7311. targ_info = ahd_fetch_transinfo(ahd,
  7312. devinfo.channel,
  7313. devinfo.our_scsiid,
  7314. devinfo.target,
  7315. &tstate);
  7316. tinfo = &targ_info->curr;
  7317. sg = scb->sg_list;
  7318. sc = (struct scsi_sense *)hscb->shared_data.idata.cdb;
  7319. /*
  7320. * Save off the residual if there is one.
  7321. */
  7322. ahd_update_residual(ahd, scb);
  7323. #ifdef AHD_DEBUG
  7324. if (ahd_debug & AHD_SHOW_SENSE) {
  7325. ahd_print_path(ahd, scb);
  7326. printf("Sending Sense\n");
  7327. }
  7328. #endif
  7329. scb->sg_count = 0;
  7330. sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
  7331. ahd_get_sense_bufsize(ahd, scb),
  7332. /*last*/TRUE);
  7333. sc->opcode = REQUEST_SENSE;
  7334. sc->byte2 = 0;
  7335. if (tinfo->protocol_version <= SCSI_REV_2
  7336. && SCB_GET_LUN(scb) < 8)
  7337. sc->byte2 = SCB_GET_LUN(scb) << 5;
  7338. sc->unused[0] = 0;
  7339. sc->unused[1] = 0;
  7340. sc->length = ahd_get_sense_bufsize(ahd, scb);
  7341. sc->control = 0;
  7342. /*
  7343. * We can't allow the target to disconnect.
  7344. * This will be an untagged transaction and
  7345. * having the target disconnect will make this
  7346. * transaction indestinguishable from outstanding
  7347. * tagged transactions.
  7348. */
  7349. hscb->control = 0;
  7350. /*
  7351. * This request sense could be because the
  7352. * the device lost power or in some other
  7353. * way has lost our transfer negotiations.
  7354. * Renegotiate if appropriate. Unit attention
  7355. * errors will be reported before any data
  7356. * phases occur.
  7357. */
  7358. if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
  7359. ahd_update_neg_request(ahd, &devinfo,
  7360. tstate, targ_info,
  7361. AHD_NEG_IF_NON_ASYNC);
  7362. }
  7363. if (tstate->auto_negotiate & devinfo.target_mask) {
  7364. hscb->control |= MK_MESSAGE;
  7365. scb->flags &=
  7366. ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
  7367. scb->flags |= SCB_AUTO_NEGOTIATE;
  7368. }
  7369. hscb->cdb_len = sizeof(*sc);
  7370. ahd_setup_data_scb(ahd, scb);
  7371. scb->flags |= SCB_SENSE;
  7372. ahd_queue_scb(ahd, scb);
  7373. /*
  7374. * Ensure we have enough time to actually
  7375. * retrieve the sense.
  7376. */
  7377. ahd_scb_timer_reset(scb, 5 * 1000000);
  7378. break;
  7379. }
  7380. case SCSI_STATUS_OK:
  7381. printf("%s: Interrupted for staus of 0???\n",
  7382. ahd_name(ahd));
  7383. /* FALLTHROUGH */
  7384. default:
  7385. ahd_done(ahd, scb);
  7386. break;
  7387. }
  7388. }
  7389. /*
  7390. * Calculate the residual for a just completed SCB.
  7391. */
  7392. void
  7393. ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
  7394. {
  7395. struct hardware_scb *hscb;
  7396. struct initiator_status *spkt;
  7397. uint32_t sgptr;
  7398. uint32_t resid_sgptr;
  7399. uint32_t resid;
  7400. /*
  7401. * 5 cases.
  7402. * 1) No residual.
  7403. * SG_STATUS_VALID clear in sgptr.
  7404. * 2) Transferless command
  7405. * 3) Never performed any transfers.
  7406. * sgptr has SG_FULL_RESID set.
  7407. * 4) No residual but target did not
  7408. * save data pointers after the
  7409. * last transfer, so sgptr was
  7410. * never updated.
  7411. * 5) We have a partial residual.
  7412. * Use residual_sgptr to determine
  7413. * where we are.
  7414. */
  7415. hscb = scb->hscb;
  7416. sgptr = ahd_le32toh(hscb->sgptr);
  7417. if ((sgptr & SG_STATUS_VALID) == 0)
  7418. /* Case 1 */
  7419. return;
  7420. sgptr &= ~SG_STATUS_VALID;
  7421. if ((sgptr & SG_LIST_NULL) != 0)
  7422. /* Case 2 */
  7423. return;
  7424. /*
  7425. * Residual fields are the same in both
  7426. * target and initiator status packets,
  7427. * so we can always use the initiator fields
  7428. * regardless of the role for this SCB.
  7429. */
  7430. spkt = &hscb->shared_data.istatus;
  7431. resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
  7432. if ((sgptr & SG_FULL_RESID) != 0) {
  7433. /* Case 3 */
  7434. resid = ahd_get_transfer_length(scb);
  7435. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  7436. /* Case 4 */
  7437. return;
  7438. } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
  7439. ahd_print_path(ahd, scb);
  7440. printf("data overrun detected Tag == 0x%x.\n",
  7441. SCB_GET_TAG(scb));
  7442. ahd_freeze_devq(ahd, scb);
  7443. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  7444. ahd_freeze_scb(scb);
  7445. return;
  7446. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  7447. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  7448. /* NOTREACHED */
  7449. } else {
  7450. struct ahd_dma_seg *sg;
  7451. /*
  7452. * Remainder of the SG where the transfer
  7453. * stopped.
  7454. */
  7455. resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
  7456. sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
  7457. /* The residual sg_ptr always points to the next sg */
  7458. sg--;
  7459. /*
  7460. * Add up the contents of all residual
  7461. * SG segments that are after the SG where
  7462. * the transfer stopped.
  7463. */
  7464. while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
  7465. sg++;
  7466. resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  7467. }
  7468. }
  7469. if ((scb->flags & SCB_SENSE) == 0)
  7470. ahd_set_residual(scb, resid);
  7471. else
  7472. ahd_set_sense_residual(scb, resid);
  7473. #ifdef AHD_DEBUG
  7474. if ((ahd_debug & AHD_SHOW_MISC) != 0) {
  7475. ahd_print_path(ahd, scb);
  7476. printf("Handled %sResidual of %d bytes\n",
  7477. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  7478. }
  7479. #endif
  7480. }
  7481. /******************************* Target Mode **********************************/
  7482. #ifdef AHD_TARGET_MODE
  7483. /*
  7484. * Add a target mode event to this lun's queue
  7485. */
  7486. static void
  7487. ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
  7488. u_int initiator_id, u_int event_type, u_int event_arg)
  7489. {
  7490. struct ahd_tmode_event *event;
  7491. int pending;
  7492. xpt_freeze_devq(lstate->path, /*count*/1);
  7493. if (lstate->event_w_idx >= lstate->event_r_idx)
  7494. pending = lstate->event_w_idx - lstate->event_r_idx;
  7495. else
  7496. pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
  7497. - (lstate->event_r_idx - lstate->event_w_idx);
  7498. if (event_type == EVENT_TYPE_BUS_RESET
  7499. || event_type == MSG_BUS_DEV_RESET) {
  7500. /*
  7501. * Any earlier events are irrelevant, so reset our buffer.
  7502. * This has the effect of allowing us to deal with reset
  7503. * floods (an external device holding down the reset line)
  7504. * without losing the event that is really interesting.
  7505. */
  7506. lstate->event_r_idx = 0;
  7507. lstate->event_w_idx = 0;
  7508. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  7509. }
  7510. if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
  7511. xpt_print_path(lstate->path);
  7512. printf("immediate event %x:%x lost\n",
  7513. lstate->event_buffer[lstate->event_r_idx].event_type,
  7514. lstate->event_buffer[lstate->event_r_idx].event_arg);
  7515. lstate->event_r_idx++;
  7516. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7517. lstate->event_r_idx = 0;
  7518. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  7519. }
  7520. event = &lstate->event_buffer[lstate->event_w_idx];
  7521. event->initiator_id = initiator_id;
  7522. event->event_type = event_type;
  7523. event->event_arg = event_arg;
  7524. lstate->event_w_idx++;
  7525. if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7526. lstate->event_w_idx = 0;
  7527. }
  7528. /*
  7529. * Send any target mode events queued up waiting
  7530. * for immediate notify resources.
  7531. */
  7532. void
  7533. ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
  7534. {
  7535. struct ccb_hdr *ccbh;
  7536. struct ccb_immed_notify *inot;
  7537. while (lstate->event_r_idx != lstate->event_w_idx
  7538. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  7539. struct ahd_tmode_event *event;
  7540. event = &lstate->event_buffer[lstate->event_r_idx];
  7541. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  7542. inot = (struct ccb_immed_notify *)ccbh;
  7543. switch (event->event_type) {
  7544. case EVENT_TYPE_BUS_RESET:
  7545. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  7546. break;
  7547. default:
  7548. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  7549. inot->message_args[0] = event->event_type;
  7550. inot->message_args[1] = event->event_arg;
  7551. break;
  7552. }
  7553. inot->initiator_id = event->initiator_id;
  7554. inot->sense_len = 0;
  7555. xpt_done((union ccb *)inot);
  7556. lstate->event_r_idx++;
  7557. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7558. lstate->event_r_idx = 0;
  7559. }
  7560. }
  7561. #endif
  7562. /******************** Sequencer Program Patching/Download *********************/
  7563. #ifdef AHD_DUMP_SEQ
  7564. void
  7565. ahd_dumpseq(struct ahd_softc* ahd)
  7566. {
  7567. int i;
  7568. int max_prog;
  7569. max_prog = 2048;
  7570. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7571. ahd_outb(ahd, PRGMCNT, 0);
  7572. ahd_outb(ahd, PRGMCNT+1, 0);
  7573. for (i = 0; i < max_prog; i++) {
  7574. uint8_t ins_bytes[4];
  7575. ahd_insb(ahd, SEQRAM, ins_bytes, 4);
  7576. printf("0x%08x\n", ins_bytes[0] << 24
  7577. | ins_bytes[1] << 16
  7578. | ins_bytes[2] << 8
  7579. | ins_bytes[3]);
  7580. }
  7581. }
  7582. #endif
  7583. static void
  7584. ahd_loadseq(struct ahd_softc *ahd)
  7585. {
  7586. struct cs cs_table[num_critical_sections];
  7587. u_int begin_set[num_critical_sections];
  7588. u_int end_set[num_critical_sections];
  7589. struct patch *cur_patch;
  7590. u_int cs_count;
  7591. u_int cur_cs;
  7592. u_int i;
  7593. int downloaded;
  7594. u_int skip_addr;
  7595. u_int sg_prefetch_cnt;
  7596. u_int sg_prefetch_cnt_limit;
  7597. u_int sg_prefetch_align;
  7598. u_int sg_size;
  7599. uint8_t download_consts[DOWNLOAD_CONST_COUNT];
  7600. if (bootverbose)
  7601. printf("%s: Downloading Sequencer Program...",
  7602. ahd_name(ahd));
  7603. #if DOWNLOAD_CONST_COUNT != 7
  7604. #error "Download Const Mismatch"
  7605. #endif
  7606. /*
  7607. * Start out with 0 critical sections
  7608. * that apply to this firmware load.
  7609. */
  7610. cs_count = 0;
  7611. cur_cs = 0;
  7612. memset(begin_set, 0, sizeof(begin_set));
  7613. memset(end_set, 0, sizeof(end_set));
  7614. /*
  7615. * Setup downloadable constant table.
  7616. *
  7617. * The computation for the S/G prefetch variables is
  7618. * a bit complicated. We would like to always fetch
  7619. * in terms of cachelined sized increments. However,
  7620. * if the cacheline is not an even multiple of the
  7621. * SG element size or is larger than our SG RAM, using
  7622. * just the cache size might leave us with only a portion
  7623. * of an SG element at the tail of a prefetch. If the
  7624. * cacheline is larger than our S/G prefetch buffer less
  7625. * the size of an SG element, we may round down to a cacheline
  7626. * that doesn't contain any or all of the S/G of interest
  7627. * within the bounds of our S/G ram. Provide variables to
  7628. * the sequencer that will allow it to handle these edge
  7629. * cases.
  7630. */
  7631. /* Start by aligning to the nearest cacheline. */
  7632. sg_prefetch_align = ahd->pci_cachesize;
  7633. if (sg_prefetch_align == 0)
  7634. sg_prefetch_align = 8;
  7635. /* Round down to the nearest power of 2. */
  7636. while (powerof2(sg_prefetch_align) == 0)
  7637. sg_prefetch_align--;
  7638. /*
  7639. * If the cacheline boundary is greater than half our prefetch RAM
  7640. * we risk not being able to fetch even a single complete S/G
  7641. * segment if we align to that boundary.
  7642. */
  7643. if (sg_prefetch_align > CCSGADDR_MAX/2)
  7644. sg_prefetch_align = CCSGADDR_MAX/2;
  7645. /* Start by fetching a single cacheline. */
  7646. sg_prefetch_cnt = sg_prefetch_align;
  7647. /*
  7648. * Increment the prefetch count by cachelines until
  7649. * at least one S/G element will fit.
  7650. */
  7651. sg_size = sizeof(struct ahd_dma_seg);
  7652. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  7653. sg_size = sizeof(struct ahd_dma64_seg);
  7654. while (sg_prefetch_cnt < sg_size)
  7655. sg_prefetch_cnt += sg_prefetch_align;
  7656. /*
  7657. * If the cacheline is not an even multiple of
  7658. * the S/G size, we may only get a partial S/G when
  7659. * we align. Add a cacheline if this is the case.
  7660. */
  7661. if ((sg_prefetch_align % sg_size) != 0
  7662. && (sg_prefetch_cnt < CCSGADDR_MAX))
  7663. sg_prefetch_cnt += sg_prefetch_align;
  7664. /*
  7665. * Lastly, compute a value that the sequencer can use
  7666. * to determine if the remainder of the CCSGRAM buffer
  7667. * has a full S/G element in it.
  7668. */
  7669. sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
  7670. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  7671. download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
  7672. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
  7673. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
  7674. download_consts[SG_SIZEOF] = sg_size;
  7675. download_consts[PKT_OVERRUN_BUFOFFSET] =
  7676. (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
  7677. download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
  7678. cur_patch = patches;
  7679. downloaded = 0;
  7680. skip_addr = 0;
  7681. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7682. ahd_outb(ahd, PRGMCNT, 0);
  7683. ahd_outb(ahd, PRGMCNT+1, 0);
  7684. for (i = 0; i < sizeof(seqprog)/4; i++) {
  7685. if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
  7686. /*
  7687. * Don't download this instruction as it
  7688. * is in a patch that was removed.
  7689. */
  7690. continue;
  7691. }
  7692. /*
  7693. * Move through the CS table until we find a CS
  7694. * that might apply to this instruction.
  7695. */
  7696. for (; cur_cs < num_critical_sections; cur_cs++) {
  7697. if (critical_sections[cur_cs].end <= i) {
  7698. if (begin_set[cs_count] == TRUE
  7699. && end_set[cs_count] == FALSE) {
  7700. cs_table[cs_count].end = downloaded;
  7701. end_set[cs_count] = TRUE;
  7702. cs_count++;
  7703. }
  7704. continue;
  7705. }
  7706. if (critical_sections[cur_cs].begin <= i
  7707. && begin_set[cs_count] == FALSE) {
  7708. cs_table[cs_count].begin = downloaded;
  7709. begin_set[cs_count] = TRUE;
  7710. }
  7711. break;
  7712. }
  7713. ahd_download_instr(ahd, i, download_consts);
  7714. downloaded++;
  7715. }
  7716. ahd->num_critical_sections = cs_count;
  7717. if (cs_count != 0) {
  7718. cs_count *= sizeof(struct cs);
  7719. ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  7720. if (ahd->critical_sections == NULL)
  7721. panic("ahd_loadseq: Could not malloc");
  7722. memcpy(ahd->critical_sections, cs_table, cs_count);
  7723. }
  7724. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
  7725. if (bootverbose) {
  7726. printf(" %d instructions downloaded\n", downloaded);
  7727. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  7728. ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
  7729. }
  7730. }
  7731. static int
  7732. ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
  7733. u_int start_instr, u_int *skip_addr)
  7734. {
  7735. struct patch *cur_patch;
  7736. struct patch *last_patch;
  7737. u_int num_patches;
  7738. num_patches = sizeof(patches)/sizeof(struct patch);
  7739. last_patch = &patches[num_patches];
  7740. cur_patch = *start_patch;
  7741. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  7742. if (cur_patch->patch_func(ahd) == 0) {
  7743. /* Start rejecting code */
  7744. *skip_addr = start_instr + cur_patch->skip_instr;
  7745. cur_patch += cur_patch->skip_patch;
  7746. } else {
  7747. /* Accepted this patch. Advance to the next
  7748. * one and wait for our intruction pointer to
  7749. * hit this point.
  7750. */
  7751. cur_patch++;
  7752. }
  7753. }
  7754. *start_patch = cur_patch;
  7755. if (start_instr < *skip_addr)
  7756. /* Still skipping */
  7757. return (0);
  7758. return (1);
  7759. }
  7760. static u_int
  7761. ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
  7762. {
  7763. struct patch *cur_patch;
  7764. int address_offset;
  7765. u_int skip_addr;
  7766. u_int i;
  7767. address_offset = 0;
  7768. cur_patch = patches;
  7769. skip_addr = 0;
  7770. for (i = 0; i < address;) {
  7771. ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
  7772. if (skip_addr > i) {
  7773. int end_addr;
  7774. end_addr = MIN(address, skip_addr);
  7775. address_offset += end_addr - i;
  7776. i = skip_addr;
  7777. } else {
  7778. i++;
  7779. }
  7780. }
  7781. return (address - address_offset);
  7782. }
  7783. static void
  7784. ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
  7785. {
  7786. union ins_formats instr;
  7787. struct ins_format1 *fmt1_ins;
  7788. struct ins_format3 *fmt3_ins;
  7789. u_int opcode;
  7790. /*
  7791. * The firmware is always compiled into a little endian format.
  7792. */
  7793. instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  7794. fmt1_ins = &instr.format1;
  7795. fmt3_ins = NULL;
  7796. /* Pull the opcode */
  7797. opcode = instr.format1.opcode;
  7798. switch (opcode) {
  7799. case AIC_OP_JMP:
  7800. case AIC_OP_JC:
  7801. case AIC_OP_JNC:
  7802. case AIC_OP_CALL:
  7803. case AIC_OP_JNE:
  7804. case AIC_OP_JNZ:
  7805. case AIC_OP_JE:
  7806. case AIC_OP_JZ:
  7807. {
  7808. fmt3_ins = &instr.format3;
  7809. fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
  7810. /* FALLTHROUGH */
  7811. }
  7812. case AIC_OP_OR:
  7813. case AIC_OP_AND:
  7814. case AIC_OP_XOR:
  7815. case AIC_OP_ADD:
  7816. case AIC_OP_ADC:
  7817. case AIC_OP_BMOV:
  7818. if (fmt1_ins->parity != 0) {
  7819. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  7820. }
  7821. fmt1_ins->parity = 0;
  7822. /* FALLTHROUGH */
  7823. case AIC_OP_ROL:
  7824. {
  7825. int i, count;
  7826. /* Calculate odd parity for the instruction */
  7827. for (i = 0, count = 0; i < 31; i++) {
  7828. uint32_t mask;
  7829. mask = 0x01 << i;
  7830. if ((instr.integer & mask) != 0)
  7831. count++;
  7832. }
  7833. if ((count & 0x01) == 0)
  7834. instr.format1.parity = 1;
  7835. /* The sequencer is a little endian cpu */
  7836. instr.integer = ahd_htole32(instr.integer);
  7837. ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
  7838. break;
  7839. }
  7840. default:
  7841. panic("Unknown opcode encountered in seq program");
  7842. break;
  7843. }
  7844. }
  7845. static int
  7846. ahd_probe_stack_size(struct ahd_softc *ahd)
  7847. {
  7848. int last_probe;
  7849. last_probe = 0;
  7850. while (1) {
  7851. int i;
  7852. /*
  7853. * We avoid using 0 as a pattern to avoid
  7854. * confusion if the stack implementation
  7855. * "back-fills" with zeros when "poping'
  7856. * entries.
  7857. */
  7858. for (i = 1; i <= last_probe+1; i++) {
  7859. ahd_outb(ahd, STACK, i & 0xFF);
  7860. ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
  7861. }
  7862. /* Verify */
  7863. for (i = last_probe+1; i > 0; i--) {
  7864. u_int stack_entry;
  7865. stack_entry = ahd_inb(ahd, STACK)
  7866. |(ahd_inb(ahd, STACK) << 8);
  7867. if (stack_entry != i)
  7868. goto sized;
  7869. }
  7870. last_probe++;
  7871. }
  7872. sized:
  7873. return (last_probe);
  7874. }
  7875. int
  7876. ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
  7877. const char *name, u_int address, u_int value,
  7878. u_int *cur_column, u_int wrap_point)
  7879. {
  7880. int printed;
  7881. u_int printed_mask;
  7882. if (cur_column != NULL && *cur_column >= wrap_point) {
  7883. printf("\n");
  7884. *cur_column = 0;
  7885. }
  7886. printed = printf("%s[0x%x]", name, value);
  7887. if (table == NULL) {
  7888. printed += printf(" ");
  7889. *cur_column += printed;
  7890. return (printed);
  7891. }
  7892. printed_mask = 0;
  7893. while (printed_mask != 0xFF) {
  7894. int entry;
  7895. for (entry = 0; entry < num_entries; entry++) {
  7896. if (((value & table[entry].mask)
  7897. != table[entry].value)
  7898. || ((printed_mask & table[entry].mask)
  7899. == table[entry].mask))
  7900. continue;
  7901. printed += printf("%s%s",
  7902. printed_mask == 0 ? ":(" : "|",
  7903. table[entry].name);
  7904. printed_mask |= table[entry].mask;
  7905. break;
  7906. }
  7907. if (entry >= num_entries)
  7908. break;
  7909. }
  7910. if (printed_mask != 0)
  7911. printed += printf(") ");
  7912. else
  7913. printed += printf(" ");
  7914. if (cur_column != NULL)
  7915. *cur_column += printed;
  7916. return (printed);
  7917. }
  7918. void
  7919. ahd_dump_card_state(struct ahd_softc *ahd)
  7920. {
  7921. struct scb *scb;
  7922. ahd_mode_state saved_modes;
  7923. u_int dffstat;
  7924. int paused;
  7925. u_int scb_index;
  7926. u_int saved_scb_index;
  7927. u_int cur_col;
  7928. int i;
  7929. if (ahd_is_paused(ahd)) {
  7930. paused = 1;
  7931. } else {
  7932. paused = 0;
  7933. ahd_pause(ahd);
  7934. }
  7935. saved_modes = ahd_save_modes(ahd);
  7936. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7937. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  7938. "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
  7939. ahd_name(ahd),
  7940. ahd_inb(ahd, CURADDR) | (ahd_inb(ahd, CURADDR+1) << 8),
  7941. ahd_build_mode_state(ahd, ahd->saved_src_mode,
  7942. ahd->saved_dst_mode));
  7943. if (paused)
  7944. printf("Card was paused\n");
  7945. if (ahd_check_cmdcmpltqueues(ahd))
  7946. printf("Completions are pending\n");
  7947. /*
  7948. * Mode independent registers.
  7949. */
  7950. cur_col = 0;
  7951. ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
  7952. ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
  7953. ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
  7954. ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
  7955. ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
  7956. ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
  7957. ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
  7958. ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
  7959. ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
  7960. ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
  7961. ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
  7962. ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
  7963. ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
  7964. ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
  7965. ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
  7966. ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
  7967. ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
  7968. ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
  7969. ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
  7970. ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
  7971. ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
  7972. ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
  7973. ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
  7974. ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
  7975. ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
  7976. ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
  7977. ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
  7978. printf("\n");
  7979. printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
  7980. "CURRSCB 0x%x NEXTSCB 0x%x\n",
  7981. ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
  7982. ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
  7983. ahd_inw(ahd, NEXTSCB));
  7984. cur_col = 0;
  7985. /* QINFIFO */
  7986. ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  7987. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  7988. ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
  7989. saved_scb_index = ahd_get_scbptr(ahd);
  7990. printf("Pending list:");
  7991. i = 0;
  7992. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  7993. if (i++ > AHD_SCB_MAX)
  7994. break;
  7995. cur_col = printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
  7996. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT));
  7997. ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
  7998. ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL),
  7999. &cur_col, 60);
  8000. ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID),
  8001. &cur_col, 60);
  8002. }
  8003. printf("\nTotal %d\n", i);
  8004. printf("Kernel Free SCB list: ");
  8005. i = 0;
  8006. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  8007. struct scb *list_scb;
  8008. list_scb = scb;
  8009. do {
  8010. printf("%d ", SCB_GET_TAG(list_scb));
  8011. list_scb = LIST_NEXT(list_scb, collision_links);
  8012. } while (list_scb && i++ < AHD_SCB_MAX);
  8013. }
  8014. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  8015. if (i++ > AHD_SCB_MAX)
  8016. break;
  8017. printf("%d ", SCB_GET_TAG(scb));
  8018. }
  8019. printf("\n");
  8020. printf("Sequencer Complete DMA-inprog list: ");
  8021. scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
  8022. i = 0;
  8023. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8024. ahd_set_scbptr(ahd, scb_index);
  8025. printf("%d ", scb_index);
  8026. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8027. }
  8028. printf("\n");
  8029. printf("Sequencer Complete list: ");
  8030. scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  8031. i = 0;
  8032. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8033. ahd_set_scbptr(ahd, scb_index);
  8034. printf("%d ", scb_index);
  8035. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8036. }
  8037. printf("\n");
  8038. printf("Sequencer DMA-Up and Complete list: ");
  8039. scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  8040. i = 0;
  8041. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8042. ahd_set_scbptr(ahd, scb_index);
  8043. printf("%d ", scb_index);
  8044. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8045. }
  8046. printf("\n");
  8047. ahd_set_scbptr(ahd, saved_scb_index);
  8048. dffstat = ahd_inb(ahd, DFFSTAT);
  8049. for (i = 0; i < 2; i++) {
  8050. #ifdef AHD_DEBUG
  8051. struct scb *fifo_scb;
  8052. #endif
  8053. u_int fifo_scbptr;
  8054. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  8055. fifo_scbptr = ahd_get_scbptr(ahd);
  8056. printf("\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
  8057. ahd_name(ahd), i,
  8058. (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
  8059. ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
  8060. cur_col = 0;
  8061. ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
  8062. ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
  8063. ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
  8064. ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
  8065. ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
  8066. &cur_col, 50);
  8067. ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
  8068. ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
  8069. ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
  8070. ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
  8071. if (cur_col > 50) {
  8072. printf("\n");
  8073. cur_col = 0;
  8074. }
  8075. cur_col += printf("SHADDR = 0x%x%x, SHCNT = 0x%x ",
  8076. ahd_inl(ahd, SHADDR+4),
  8077. ahd_inl(ahd, SHADDR),
  8078. (ahd_inb(ahd, SHCNT)
  8079. | (ahd_inb(ahd, SHCNT + 1) << 8)
  8080. | (ahd_inb(ahd, SHCNT + 2) << 16)));
  8081. if (cur_col > 50) {
  8082. printf("\n");
  8083. cur_col = 0;
  8084. }
  8085. cur_col += printf("HADDR = 0x%x%x, HCNT = 0x%x ",
  8086. ahd_inl(ahd, HADDR+4),
  8087. ahd_inl(ahd, HADDR),
  8088. (ahd_inb(ahd, HCNT)
  8089. | (ahd_inb(ahd, HCNT + 1) << 8)
  8090. | (ahd_inb(ahd, HCNT + 2) << 16)));
  8091. ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
  8092. #ifdef AHD_DEBUG
  8093. if ((ahd_debug & AHD_SHOW_SG) != 0) {
  8094. fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
  8095. if (fifo_scb != NULL)
  8096. ahd_dump_sglist(fifo_scb);
  8097. }
  8098. #endif
  8099. }
  8100. printf("\nLQIN: ");
  8101. for (i = 0; i < 20; i++)
  8102. printf("0x%x ", ahd_inb(ahd, LQIN + i));
  8103. printf("\n");
  8104. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  8105. printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
  8106. ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
  8107. ahd_inb(ahd, OPTIONMODE));
  8108. printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
  8109. ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
  8110. ahd_inb(ahd, MAXCMDCNT));
  8111. ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
  8112. printf("\n");
  8113. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  8114. cur_col = 0;
  8115. ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50);
  8116. printf("\n");
  8117. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  8118. printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
  8119. ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
  8120. ahd_inw(ahd, DINDEX));
  8121. printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
  8122. ahd_name(ahd), ahd_get_scbptr(ahd),
  8123. ahd_inw_scbram(ahd, SCB_NEXT),
  8124. ahd_inw_scbram(ahd, SCB_NEXT2));
  8125. printf("CDB %x %x %x %x %x %x\n",
  8126. ahd_inb_scbram(ahd, SCB_CDB_STORE),
  8127. ahd_inb_scbram(ahd, SCB_CDB_STORE+1),
  8128. ahd_inb_scbram(ahd, SCB_CDB_STORE+2),
  8129. ahd_inb_scbram(ahd, SCB_CDB_STORE+3),
  8130. ahd_inb_scbram(ahd, SCB_CDB_STORE+4),
  8131. ahd_inb_scbram(ahd, SCB_CDB_STORE+5));
  8132. printf("STACK:");
  8133. for (i = 0; i < ahd->stack_size; i++) {
  8134. ahd->saved_stack[i] =
  8135. ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
  8136. printf(" 0x%x", ahd->saved_stack[i]);
  8137. }
  8138. for (i = ahd->stack_size-1; i >= 0; i--) {
  8139. ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
  8140. ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
  8141. }
  8142. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  8143. ahd_restore_modes(ahd, saved_modes);
  8144. if (paused == 0)
  8145. ahd_unpause(ahd);
  8146. }
  8147. void
  8148. ahd_dump_scbs(struct ahd_softc *ahd)
  8149. {
  8150. ahd_mode_state saved_modes;
  8151. u_int saved_scb_index;
  8152. int i;
  8153. saved_modes = ahd_save_modes(ahd);
  8154. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  8155. saved_scb_index = ahd_get_scbptr(ahd);
  8156. for (i = 0; i < AHD_SCB_MAX; i++) {
  8157. ahd_set_scbptr(ahd, i);
  8158. printf("%3d", i);
  8159. printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
  8160. ahd_inb_scbram(ahd, SCB_CONTROL),
  8161. ahd_inb_scbram(ahd, SCB_SCSIID),
  8162. ahd_inw_scbram(ahd, SCB_NEXT),
  8163. ahd_inw_scbram(ahd, SCB_NEXT2),
  8164. ahd_inl_scbram(ahd, SCB_SGPTR),
  8165. ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR));
  8166. }
  8167. printf("\n");
  8168. ahd_set_scbptr(ahd, saved_scb_index);
  8169. ahd_restore_modes(ahd, saved_modes);
  8170. }
  8171. /**************************** Flexport Logic **********************************/
  8172. /*
  8173. * Read count 16bit words from 16bit word address start_addr from the
  8174. * SEEPROM attached to the controller, into buf, using the controller's
  8175. * SEEPROM reading state machine. Optionally treat the data as a byte
  8176. * stream in terms of byte order.
  8177. */
  8178. int
  8179. ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8180. u_int start_addr, u_int count, int bytestream)
  8181. {
  8182. u_int cur_addr;
  8183. u_int end_addr;
  8184. int error;
  8185. /*
  8186. * If we never make it through the loop even once,
  8187. * we were passed invalid arguments.
  8188. */
  8189. error = EINVAL;
  8190. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8191. end_addr = start_addr + count;
  8192. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8193. ahd_outb(ahd, SEEADR, cur_addr);
  8194. ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
  8195. error = ahd_wait_seeprom(ahd);
  8196. if (error)
  8197. break;
  8198. if (bytestream != 0) {
  8199. uint8_t *bytestream_ptr;
  8200. bytestream_ptr = (uint8_t *)buf;
  8201. *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
  8202. *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
  8203. } else {
  8204. /*
  8205. * ahd_inw() already handles machine byte order.
  8206. */
  8207. *buf = ahd_inw(ahd, SEEDAT);
  8208. }
  8209. buf++;
  8210. }
  8211. return (error);
  8212. }
  8213. /*
  8214. * Write count 16bit words from buf, into SEEPROM attache to the
  8215. * controller starting at 16bit word address start_addr, using the
  8216. * controller's SEEPROM writing state machine.
  8217. */
  8218. int
  8219. ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8220. u_int start_addr, u_int count)
  8221. {
  8222. u_int cur_addr;
  8223. u_int end_addr;
  8224. int error;
  8225. int retval;
  8226. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8227. error = ENOENT;
  8228. /* Place the chip into write-enable mode */
  8229. ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
  8230. ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
  8231. error = ahd_wait_seeprom(ahd);
  8232. if (error)
  8233. return (error);
  8234. /*
  8235. * Write the data. If we don't get throught the loop at
  8236. * least once, the arguments were invalid.
  8237. */
  8238. retval = EINVAL;
  8239. end_addr = start_addr + count;
  8240. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8241. ahd_outw(ahd, SEEDAT, *buf++);
  8242. ahd_outb(ahd, SEEADR, cur_addr);
  8243. ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
  8244. retval = ahd_wait_seeprom(ahd);
  8245. if (retval)
  8246. break;
  8247. }
  8248. /*
  8249. * Disable writes.
  8250. */
  8251. ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
  8252. ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
  8253. error = ahd_wait_seeprom(ahd);
  8254. if (error)
  8255. return (error);
  8256. return (retval);
  8257. }
  8258. /*
  8259. * Wait ~100us for the serial eeprom to satisfy our request.
  8260. */
  8261. int
  8262. ahd_wait_seeprom(struct ahd_softc *ahd)
  8263. {
  8264. int cnt;
  8265. cnt = 20;
  8266. while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
  8267. ahd_delay(5);
  8268. if (cnt == 0)
  8269. return (ETIMEDOUT);
  8270. return (0);
  8271. }
  8272. /*
  8273. * Validate the two checksums in the per_channel
  8274. * vital product data struct.
  8275. */
  8276. int
  8277. ahd_verify_vpd_cksum(struct vpd_config *vpd)
  8278. {
  8279. int i;
  8280. int maxaddr;
  8281. uint32_t checksum;
  8282. uint8_t *vpdarray;
  8283. vpdarray = (uint8_t *)vpd;
  8284. maxaddr = offsetof(struct vpd_config, vpd_checksum);
  8285. checksum = 0;
  8286. for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
  8287. checksum = checksum + vpdarray[i];
  8288. if (checksum == 0
  8289. || (-checksum & 0xFF) != vpd->vpd_checksum)
  8290. return (0);
  8291. checksum = 0;
  8292. maxaddr = offsetof(struct vpd_config, checksum);
  8293. for (i = offsetof(struct vpd_config, default_target_flags);
  8294. i < maxaddr; i++)
  8295. checksum = checksum + vpdarray[i];
  8296. if (checksum == 0
  8297. || (-checksum & 0xFF) != vpd->checksum)
  8298. return (0);
  8299. return (1);
  8300. }
  8301. int
  8302. ahd_verify_cksum(struct seeprom_config *sc)
  8303. {
  8304. int i;
  8305. int maxaddr;
  8306. uint32_t checksum;
  8307. uint16_t *scarray;
  8308. maxaddr = (sizeof(*sc)/2) - 1;
  8309. checksum = 0;
  8310. scarray = (uint16_t *)sc;
  8311. for (i = 0; i < maxaddr; i++)
  8312. checksum = checksum + scarray[i];
  8313. if (checksum == 0
  8314. || (checksum & 0xFFFF) != sc->checksum) {
  8315. return (0);
  8316. } else {
  8317. return (1);
  8318. }
  8319. }
  8320. int
  8321. ahd_acquire_seeprom(struct ahd_softc *ahd)
  8322. {
  8323. /*
  8324. * We should be able to determine the SEEPROM type
  8325. * from the flexport logic, but unfortunately not
  8326. * all implementations have this logic and there is
  8327. * no programatic method for determining if the logic
  8328. * is present.
  8329. */
  8330. return (1);
  8331. #if 0
  8332. uint8_t seetype;
  8333. int error;
  8334. error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
  8335. if (error != 0
  8336. || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
  8337. return (0);
  8338. return (1);
  8339. #endif
  8340. }
  8341. void
  8342. ahd_release_seeprom(struct ahd_softc *ahd)
  8343. {
  8344. /* Currently a no-op */
  8345. }
  8346. int
  8347. ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
  8348. {
  8349. int error;
  8350. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8351. if (addr > 7)
  8352. panic("ahd_write_flexport: address out of range");
  8353. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8354. error = ahd_wait_flexport(ahd);
  8355. if (error != 0)
  8356. return (error);
  8357. ahd_outb(ahd, BRDDAT, value);
  8358. ahd_flush_device_writes(ahd);
  8359. ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
  8360. ahd_flush_device_writes(ahd);
  8361. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8362. ahd_flush_device_writes(ahd);
  8363. ahd_outb(ahd, BRDCTL, 0);
  8364. ahd_flush_device_writes(ahd);
  8365. return (0);
  8366. }
  8367. int
  8368. ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
  8369. {
  8370. int error;
  8371. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8372. if (addr > 7)
  8373. panic("ahd_read_flexport: address out of range");
  8374. ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
  8375. error = ahd_wait_flexport(ahd);
  8376. if (error != 0)
  8377. return (error);
  8378. *value = ahd_inb(ahd, BRDDAT);
  8379. ahd_outb(ahd, BRDCTL, 0);
  8380. ahd_flush_device_writes(ahd);
  8381. return (0);
  8382. }
  8383. /*
  8384. * Wait at most 2 seconds for flexport arbitration to succeed.
  8385. */
  8386. int
  8387. ahd_wait_flexport(struct ahd_softc *ahd)
  8388. {
  8389. int cnt;
  8390. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8391. cnt = 1000000 * 2 / 5;
  8392. while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
  8393. ahd_delay(5);
  8394. if (cnt == 0)
  8395. return (ETIMEDOUT);
  8396. return (0);
  8397. }
  8398. /************************* Target Mode ****************************************/
  8399. #ifdef AHD_TARGET_MODE
  8400. cam_status
  8401. ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
  8402. struct ahd_tmode_tstate **tstate,
  8403. struct ahd_tmode_lstate **lstate,
  8404. int notfound_failure)
  8405. {
  8406. if ((ahd->features & AHD_TARGETMODE) == 0)
  8407. return (CAM_REQ_INVALID);
  8408. /*
  8409. * Handle the 'black hole' device that sucks up
  8410. * requests to unattached luns on enabled targets.
  8411. */
  8412. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  8413. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  8414. *tstate = NULL;
  8415. *lstate = ahd->black_hole;
  8416. } else {
  8417. u_int max_id;
  8418. max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
  8419. if (ccb->ccb_h.target_id > max_id)
  8420. return (CAM_TID_INVALID);
  8421. if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
  8422. return (CAM_LUN_INVALID);
  8423. *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
  8424. *lstate = NULL;
  8425. if (*tstate != NULL)
  8426. *lstate =
  8427. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  8428. }
  8429. if (notfound_failure != 0 && *lstate == NULL)
  8430. return (CAM_PATH_INVALID);
  8431. return (CAM_REQ_CMP);
  8432. }
  8433. void
  8434. ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
  8435. {
  8436. #if NOT_YET
  8437. struct ahd_tmode_tstate *tstate;
  8438. struct ahd_tmode_lstate *lstate;
  8439. struct ccb_en_lun *cel;
  8440. cam_status status;
  8441. u_int target;
  8442. u_int lun;
  8443. u_int target_mask;
  8444. u_long s;
  8445. char channel;
  8446. status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
  8447. /*notfound_failure*/FALSE);
  8448. if (status != CAM_REQ_CMP) {
  8449. ccb->ccb_h.status = status;
  8450. return;
  8451. }
  8452. if ((ahd->features & AHD_MULTIROLE) != 0) {
  8453. u_int our_id;
  8454. our_id = ahd->our_id;
  8455. if (ccb->ccb_h.target_id != our_id) {
  8456. if ((ahd->features & AHD_MULTI_TID) != 0
  8457. && (ahd->flags & AHD_INITIATORROLE) != 0) {
  8458. /*
  8459. * Only allow additional targets if
  8460. * the initiator role is disabled.
  8461. * The hardware cannot handle a re-select-in
  8462. * on the initiator id during a re-select-out
  8463. * on a different target id.
  8464. */
  8465. status = CAM_TID_INVALID;
  8466. } else if ((ahd->flags & AHD_INITIATORROLE) != 0
  8467. || ahd->enabled_luns > 0) {
  8468. /*
  8469. * Only allow our target id to change
  8470. * if the initiator role is not configured
  8471. * and there are no enabled luns which
  8472. * are attached to the currently registered
  8473. * scsi id.
  8474. */
  8475. status = CAM_TID_INVALID;
  8476. }
  8477. }
  8478. }
  8479. if (status != CAM_REQ_CMP) {
  8480. ccb->ccb_h.status = status;
  8481. return;
  8482. }
  8483. /*
  8484. * We now have an id that is valid.
  8485. * If we aren't in target mode, switch modes.
  8486. */
  8487. if ((ahd->flags & AHD_TARGETROLE) == 0
  8488. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  8489. u_long s;
  8490. printf("Configuring Target Mode\n");
  8491. ahd_lock(ahd, &s);
  8492. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  8493. ccb->ccb_h.status = CAM_BUSY;
  8494. ahd_unlock(ahd, &s);
  8495. return;
  8496. }
  8497. ahd->flags |= AHD_TARGETROLE;
  8498. if ((ahd->features & AHD_MULTIROLE) == 0)
  8499. ahd->flags &= ~AHD_INITIATORROLE;
  8500. ahd_pause(ahd);
  8501. ahd_loadseq(ahd);
  8502. ahd_restart(ahd);
  8503. ahd_unlock(ahd, &s);
  8504. }
  8505. cel = &ccb->cel;
  8506. target = ccb->ccb_h.target_id;
  8507. lun = ccb->ccb_h.target_lun;
  8508. channel = SIM_CHANNEL(ahd, sim);
  8509. target_mask = 0x01 << target;
  8510. if (channel == 'B')
  8511. target_mask <<= 8;
  8512. if (cel->enable != 0) {
  8513. u_int scsiseq1;
  8514. /* Are we already enabled?? */
  8515. if (lstate != NULL) {
  8516. xpt_print_path(ccb->ccb_h.path);
  8517. printf("Lun already enabled\n");
  8518. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  8519. return;
  8520. }
  8521. if (cel->grp6_len != 0
  8522. || cel->grp7_len != 0) {
  8523. /*
  8524. * Don't (yet?) support vendor
  8525. * specific commands.
  8526. */
  8527. ccb->ccb_h.status = CAM_REQ_INVALID;
  8528. printf("Non-zero Group Codes\n");
  8529. return;
  8530. }
  8531. /*
  8532. * Seems to be okay.
  8533. * Setup our data structures.
  8534. */
  8535. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  8536. tstate = ahd_alloc_tstate(ahd, target, channel);
  8537. if (tstate == NULL) {
  8538. xpt_print_path(ccb->ccb_h.path);
  8539. printf("Couldn't allocate tstate\n");
  8540. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8541. return;
  8542. }
  8543. }
  8544. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  8545. if (lstate == NULL) {
  8546. xpt_print_path(ccb->ccb_h.path);
  8547. printf("Couldn't allocate lstate\n");
  8548. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8549. return;
  8550. }
  8551. memset(lstate, 0, sizeof(*lstate));
  8552. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  8553. xpt_path_path_id(ccb->ccb_h.path),
  8554. xpt_path_target_id(ccb->ccb_h.path),
  8555. xpt_path_lun_id(ccb->ccb_h.path));
  8556. if (status != CAM_REQ_CMP) {
  8557. free(lstate, M_DEVBUF);
  8558. xpt_print_path(ccb->ccb_h.path);
  8559. printf("Couldn't allocate path\n");
  8560. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8561. return;
  8562. }
  8563. SLIST_INIT(&lstate->accept_tios);
  8564. SLIST_INIT(&lstate->immed_notifies);
  8565. ahd_lock(ahd, &s);
  8566. ahd_pause(ahd);
  8567. if (target != CAM_TARGET_WILDCARD) {
  8568. tstate->enabled_luns[lun] = lstate;
  8569. ahd->enabled_luns++;
  8570. if ((ahd->features & AHD_MULTI_TID) != 0) {
  8571. u_int targid_mask;
  8572. targid_mask = ahd_inb(ahd, TARGID)
  8573. | (ahd_inb(ahd, TARGID + 1) << 8);
  8574. targid_mask |= target_mask;
  8575. ahd_outb(ahd, TARGID, targid_mask);
  8576. ahd_outb(ahd, TARGID+1, (targid_mask >> 8));
  8577. ahd_update_scsiid(ahd, targid_mask);
  8578. } else {
  8579. u_int our_id;
  8580. char channel;
  8581. channel = SIM_CHANNEL(ahd, sim);
  8582. our_id = SIM_SCSI_ID(ahd, sim);
  8583. /*
  8584. * This can only happen if selections
  8585. * are not enabled
  8586. */
  8587. if (target != our_id) {
  8588. u_int sblkctl;
  8589. char cur_channel;
  8590. int swap;
  8591. sblkctl = ahd_inb(ahd, SBLKCTL);
  8592. cur_channel = (sblkctl & SELBUSB)
  8593. ? 'B' : 'A';
  8594. if ((ahd->features & AHD_TWIN) == 0)
  8595. cur_channel = 'A';
  8596. swap = cur_channel != channel;
  8597. ahd->our_id = target;
  8598. if (swap)
  8599. ahd_outb(ahd, SBLKCTL,
  8600. sblkctl ^ SELBUSB);
  8601. ahd_outb(ahd, SCSIID, target);
  8602. if (swap)
  8603. ahd_outb(ahd, SBLKCTL, sblkctl);
  8604. }
  8605. }
  8606. } else
  8607. ahd->black_hole = lstate;
  8608. /* Allow select-in operations */
  8609. if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
  8610. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8611. scsiseq1 |= ENSELI;
  8612. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8613. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8614. scsiseq1 |= ENSELI;
  8615. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8616. }
  8617. ahd_unpause(ahd);
  8618. ahd_unlock(ahd, &s);
  8619. ccb->ccb_h.status = CAM_REQ_CMP;
  8620. xpt_print_path(ccb->ccb_h.path);
  8621. printf("Lun now enabled for target mode\n");
  8622. } else {
  8623. struct scb *scb;
  8624. int i, empty;
  8625. if (lstate == NULL) {
  8626. ccb->ccb_h.status = CAM_LUN_INVALID;
  8627. return;
  8628. }
  8629. ahd_lock(ahd, &s);
  8630. ccb->ccb_h.status = CAM_REQ_CMP;
  8631. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  8632. struct ccb_hdr *ccbh;
  8633. ccbh = &scb->io_ctx->ccb_h;
  8634. if (ccbh->func_code == XPT_CONT_TARGET_IO
  8635. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  8636. printf("CTIO pending\n");
  8637. ccb->ccb_h.status = CAM_REQ_INVALID;
  8638. ahd_unlock(ahd, &s);
  8639. return;
  8640. }
  8641. }
  8642. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  8643. printf("ATIOs pending\n");
  8644. ccb->ccb_h.status = CAM_REQ_INVALID;
  8645. }
  8646. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  8647. printf("INOTs pending\n");
  8648. ccb->ccb_h.status = CAM_REQ_INVALID;
  8649. }
  8650. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  8651. ahd_unlock(ahd, &s);
  8652. return;
  8653. }
  8654. xpt_print_path(ccb->ccb_h.path);
  8655. printf("Target mode disabled\n");
  8656. xpt_free_path(lstate->path);
  8657. free(lstate, M_DEVBUF);
  8658. ahd_pause(ahd);
  8659. /* Can we clean up the target too? */
  8660. if (target != CAM_TARGET_WILDCARD) {
  8661. tstate->enabled_luns[lun] = NULL;
  8662. ahd->enabled_luns--;
  8663. for (empty = 1, i = 0; i < 8; i++)
  8664. if (tstate->enabled_luns[i] != NULL) {
  8665. empty = 0;
  8666. break;
  8667. }
  8668. if (empty) {
  8669. ahd_free_tstate(ahd, target, channel,
  8670. /*force*/FALSE);
  8671. if (ahd->features & AHD_MULTI_TID) {
  8672. u_int targid_mask;
  8673. targid_mask = ahd_inb(ahd, TARGID)
  8674. | (ahd_inb(ahd, TARGID + 1)
  8675. << 8);
  8676. targid_mask &= ~target_mask;
  8677. ahd_outb(ahd, TARGID, targid_mask);
  8678. ahd_outb(ahd, TARGID+1,
  8679. (targid_mask >> 8));
  8680. ahd_update_scsiid(ahd, targid_mask);
  8681. }
  8682. }
  8683. } else {
  8684. ahd->black_hole = NULL;
  8685. /*
  8686. * We can't allow selections without
  8687. * our black hole device.
  8688. */
  8689. empty = TRUE;
  8690. }
  8691. if (ahd->enabled_luns == 0) {
  8692. /* Disallow select-in */
  8693. u_int scsiseq1;
  8694. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8695. scsiseq1 &= ~ENSELI;
  8696. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8697. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8698. scsiseq1 &= ~ENSELI;
  8699. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8700. if ((ahd->features & AHD_MULTIROLE) == 0) {
  8701. printf("Configuring Initiator Mode\n");
  8702. ahd->flags &= ~AHD_TARGETROLE;
  8703. ahd->flags |= AHD_INITIATORROLE;
  8704. ahd_pause(ahd);
  8705. ahd_loadseq(ahd);
  8706. ahd_restart(ahd);
  8707. /*
  8708. * Unpaused. The extra unpause
  8709. * that follows is harmless.
  8710. */
  8711. }
  8712. }
  8713. ahd_unpause(ahd);
  8714. ahd_unlock(ahd, &s);
  8715. }
  8716. #endif
  8717. }
  8718. static void
  8719. ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
  8720. {
  8721. #if NOT_YET
  8722. u_int scsiid_mask;
  8723. u_int scsiid;
  8724. if ((ahd->features & AHD_MULTI_TID) == 0)
  8725. panic("ahd_update_scsiid called on non-multitid unit\n");
  8726. /*
  8727. * Since we will rely on the TARGID mask
  8728. * for selection enables, ensure that OID
  8729. * in SCSIID is not set to some other ID
  8730. * that we don't want to allow selections on.
  8731. */
  8732. if ((ahd->features & AHD_ULTRA2) != 0)
  8733. scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
  8734. else
  8735. scsiid = ahd_inb(ahd, SCSIID);
  8736. scsiid_mask = 0x1 << (scsiid & OID);
  8737. if ((targid_mask & scsiid_mask) == 0) {
  8738. u_int our_id;
  8739. /* ffs counts from 1 */
  8740. our_id = ffs(targid_mask);
  8741. if (our_id == 0)
  8742. our_id = ahd->our_id;
  8743. else
  8744. our_id--;
  8745. scsiid &= TID;
  8746. scsiid |= our_id;
  8747. }
  8748. if ((ahd->features & AHD_ULTRA2) != 0)
  8749. ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
  8750. else
  8751. ahd_outb(ahd, SCSIID, scsiid);
  8752. #endif
  8753. }
  8754. void
  8755. ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
  8756. {
  8757. struct target_cmd *cmd;
  8758. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
  8759. while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
  8760. /*
  8761. * Only advance through the queue if we
  8762. * have the resources to process the command.
  8763. */
  8764. if (ahd_handle_target_cmd(ahd, cmd) != 0)
  8765. break;
  8766. cmd->cmd_valid = 0;
  8767. ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
  8768. ahd->shared_data_dmamap,
  8769. ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
  8770. sizeof(struct target_cmd),
  8771. BUS_DMASYNC_PREREAD);
  8772. ahd->tqinfifonext++;
  8773. /*
  8774. * Lazily update our position in the target mode incoming
  8775. * command queue as seen by the sequencer.
  8776. */
  8777. if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  8778. u_int hs_mailbox;
  8779. hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
  8780. hs_mailbox &= ~HOST_TQINPOS;
  8781. hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
  8782. ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
  8783. }
  8784. }
  8785. }
  8786. static int
  8787. ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
  8788. {
  8789. struct ahd_tmode_tstate *tstate;
  8790. struct ahd_tmode_lstate *lstate;
  8791. struct ccb_accept_tio *atio;
  8792. uint8_t *byte;
  8793. int initiator;
  8794. int target;
  8795. int lun;
  8796. initiator = SCSIID_TARGET(ahd, cmd->scsiid);
  8797. target = SCSIID_OUR_ID(cmd->scsiid);
  8798. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  8799. byte = cmd->bytes;
  8800. tstate = ahd->enabled_targets[target];
  8801. lstate = NULL;
  8802. if (tstate != NULL)
  8803. lstate = tstate->enabled_luns[lun];
  8804. /*
  8805. * Commands for disabled luns go to the black hole driver.
  8806. */
  8807. if (lstate == NULL)
  8808. lstate = ahd->black_hole;
  8809. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  8810. if (atio == NULL) {
  8811. ahd->flags |= AHD_TQINFIFO_BLOCKED;
  8812. /*
  8813. * Wait for more ATIOs from the peripheral driver for this lun.
  8814. */
  8815. return (1);
  8816. } else
  8817. ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
  8818. #ifdef AHD_DEBUG
  8819. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  8820. printf("Incoming command from %d for %d:%d%s\n",
  8821. initiator, target, lun,
  8822. lstate == ahd->black_hole ? "(Black Holed)" : "");
  8823. #endif
  8824. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  8825. if (lstate == ahd->black_hole) {
  8826. /* Fill in the wildcards */
  8827. atio->ccb_h.target_id = target;
  8828. atio->ccb_h.target_lun = lun;
  8829. }
  8830. /*
  8831. * Package it up and send it off to
  8832. * whomever has this lun enabled.
  8833. */
  8834. atio->sense_len = 0;
  8835. atio->init_id = initiator;
  8836. if (byte[0] != 0xFF) {
  8837. /* Tag was included */
  8838. atio->tag_action = *byte++;
  8839. atio->tag_id = *byte++;
  8840. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  8841. } else {
  8842. atio->ccb_h.flags = 0;
  8843. }
  8844. byte++;
  8845. /* Okay. Now determine the cdb size based on the command code */
  8846. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  8847. case 0:
  8848. atio->cdb_len = 6;
  8849. break;
  8850. case 1:
  8851. case 2:
  8852. atio->cdb_len = 10;
  8853. break;
  8854. case 4:
  8855. atio->cdb_len = 16;
  8856. break;
  8857. case 5:
  8858. atio->cdb_len = 12;
  8859. break;
  8860. case 3:
  8861. default:
  8862. /* Only copy the opcode. */
  8863. atio->cdb_len = 1;
  8864. printf("Reserved or VU command code type encountered\n");
  8865. break;
  8866. }
  8867. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  8868. atio->ccb_h.status |= CAM_CDB_RECVD;
  8869. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  8870. /*
  8871. * We weren't allowed to disconnect.
  8872. * We're hanging on the bus until a
  8873. * continue target I/O comes in response
  8874. * to this accept tio.
  8875. */
  8876. #ifdef AHD_DEBUG
  8877. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  8878. printf("Received Immediate Command %d:%d:%d - %p\n",
  8879. initiator, target, lun, ahd->pending_device);
  8880. #endif
  8881. ahd->pending_device = lstate;
  8882. ahd_freeze_ccb((union ccb *)atio);
  8883. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  8884. }
  8885. xpt_done((union ccb*)atio);
  8886. return (0);
  8887. }
  8888. #endif