aic79xx_core.c 269 KB

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