aic79xx_core.c 270 KB

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